图片文件上传

来源:互联网 发布:java集成开发环境 编辑:程序博客网 时间:2024/04/28 09:55


private File headImg;   //文件接收
private String headImgContentType;  // 图像类型
private String headImgFileName;     // 图像名称



// 处理图像
if(headImg != null){
// 1、保存图像到upload/user
String filePath = ServletActionContext.getServletContext().getRealPath("upload/user");
// 2、获取保存路径的绝对值
String fileName = UUID.randomUUID().toString().replace("-", "")+ headImgFileName.substring(headImgFileName.lastIndexOf(".")) ;
// 3、复制文件
FileUtils.copyFile(headImg, new File(filePath,fileName));
// 4、设置用户头像路径
user.setHeadImg("user/"+fileName);
}
userService.save(user);
}
0 0