SSH改变用户头像
来源:互联网 发布:达芬奇视频编辑软件 编辑:程序博客网 时间:2024/06/08 20:14
这个需求在大部分网站中都有体现,就是改变用户的头像,用户随意上传一张图片,然后以后还可以修改头像。
一般网站是这样做的:注册时不需要设置头像,然后当用户使用多了,便可以自由改变原有的默认头像。
这个实现起来很简单:
代码已经上传,使用时注意几点
1.struts.xml 中的配置 ,基本就这样写,showUsers指的是一个Action,表示改完图片后,执行的展示全部用户信息的Action。
<action name="upUser" class="com.action.UpUserAction"><interceptor-ref name="fileUpload"><param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param><param name="maximumSize">1024*1024</param></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref><param name="savePath">/upload</param><result name="success" type="chain">showUsers</result><result name="input">/upUser.jsp</result></action>UtilMethod.java(一个工具类,辅助实现JSP和文件的上传)
package com.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.dao.UserDao;import com.domain.User;public class UtilMethod {public static List<User> findAll() //表示静态调出全部用户信息{ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");UserDao userDao=(UserDao) context.getBean("userDao");return userDao.findAll();}public static void close(FileOutputStream fos, FileInputStream fis) { if (fis != null) { try { fis.close(); } catch (IOException e) { System.out.println("FileInputStream关闭失败"); e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { System.out.println("FileOutputStream关闭失败"); e.printStackTrace(); } } }public static void Upload(String uploadPath,String SaveName,File file){ //辅助类,实现文件的生成。if (!(new File(uploadPath)).exists()) {new File(uploadPath).mkdirs();}File saveFile=new File(uploadPath,SaveName);FileOutputStream fos = null;FileInputStream fis = null;try{fos = new FileOutputStream(saveFile); fis = new FileInputStream(file); //设置缓存 byte[] buffer = new byte[1024]; int length = 0; //读取File文件输出到saveFile文件中 while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } }catch(Exception e){System.out.println("文件上传失败"); e.printStackTrace();} finally {UtilMethod.close(fos, fis); }}}applicationContext.xml中核心
配置user是为了提交修改页面的数据后,可以在UpUser中自动把表单数据填充到user对象中。
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userDao" class="com.dao.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="user" class="com.domain.User"></bean>UpUserAction.java
package com.action;import java.io.File;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import org.apache.struts2.ServletActionContext;import com.dao.UserDao;import com.domain.User;import com.opensymphony.xwork2.ActionSupport;import com.util.UtilMethod;public class UpUserAction extends ActionSupport {private static final long serialVersionUID = 1L;private UserDao userDao; //数据源private User user;//把标单数据自动注入到此JavaBean中private File file;private String fileFileName; private String postId;public UserDao getUserDao() {return userDao;}public void setUserDao(UserDao userDao) {this.userDao = userDao;}public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getFileFileName() {return fileFileName;}public void setFileFileName(String fileFileName) {this.fileFileName = fileFileName;}public String getPostId() {return postId;}public void setPostId(String postId) {this.postId = postId;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}@Overridepublic String execute(){List<User>users=userDao.findAll();User old=new User();for(User u:users) {if(u.getId().intValue()==user.getId().intValue()) //JSP中隐藏了user的Id数据,即这一项不可改变,找到原来的User,得到原来的数据{old=u;break;}}old.setName(user.getName()); //把表单中的数据填充到原来的user中。String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");//设置日期格式String msg =sdf.format(new Date(System.currentTimeMillis()));if(file==null || fileFileName==null){user.setPhoto("default.jpg");}else{if(!old.getPhoto().equals("default.jpg")) //把过去使用的图片删除,但不会删除默认的图片{Path del=Paths.get(uploadPath+"\\"+user.getPhoto()); System.out.println("被删除的图片位置是:"+uploadPath+"\\"+user.getPhoto());try {Files.deleteIfExists(del);} catch (IOException e) {System.out.println("原来的图片没有删除!!!");e.printStackTrace();}}String param = fileFileName.substring(fileFileName.lastIndexOf("."));String SaveName = msg + param; UtilMethod.Upload(uploadPath, SaveName, file);old.setPhoto(SaveName);}userDao.update(old);return SUCCESS;}}基本上便是这样实现,具体请参考源码:点我获得源码
0 0
- SSH改变用户头像
- 点击头像,调用相机,相册改变头像
- 网站用户头像管理
- 地图显示用户头像
- android用户头像截取
- 设置用户圆形头像
- 用户头像选择功能模块
- Android更改用户头像
- android用户头像截取
- 修改用户头像
- 用户 用户头像 数据库设计
- 用户 用户头像 数据库设计
- ssh中上传头像文件
- 用户头像的背景为头像的模糊头像时候
- Discuz修改默认用户头像
- 圆形DIV之用户头像
- Web实战之用户头像
- Android设置用户头像代码
- Struts2——验证方法
- Numpy中找出array中最大值所对应的行和列
- sudo管理
- 用机器学习对CTR预估建模(一)
- hdoj 2017
- SSH改变用户头像
- 蓝桥杯算法提高 复数归一化
- NumPy数组(1、数组初探)
- NumPy简明教程(二、数组3)
- NumPy数组(2、数组的操作)
- Rails多层关系子模型验证错误在父模型中的处理
- java并发每日一练
- 【黑苹果教程】Sierra出现“Could not load Displays preference pane”
- 谷哥的小弟学后台(07)——XML和JUnit