java用什么形式做aPP是上传图片的接口
java用什么方式做aPP是上传图片的接口
java用什么方式做aPP上传图片的接口, 江湖救急啊![java用什么形式做aPP是上传图片的接口 java用什么形式做aPP是上传图片的接口](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE1LzEwLzE2LzEwMzYzODYuZ2lm)
------解决思路----------------------
和jsp一样的么
------解决思路----------------------
MultipartFile file
------解决思路----------------------
java用什么方式做aPP上传图片的接口, 江湖救急啊
------解决思路----------------------
和jsp一样的么
------解决思路----------------------
MultipartFile file
------解决思路----------------------
@RequestMapping("upload")
public String uploadUserImage(HttpServletRequest req, @RequestParam(value = "file", required = false) MultipartFile file) {
long st = new Date().getTime();
log.info("Action开始");
log.info("上传用户图像");
String rootPath = req.getSession().getServletContext().getRealPath("/");
HttpSession session = req.getSession();
UserInfoBean tmpuser = (UserInfoBean) session.getAttribute("userInfo");
UserInfoBean item = userService.loadUserInfoByUid(tmpuser.getUid());
// 上传新的图像,删除原来的图像
File df = new File(rootPath + item.getImgpath());
if (df.exists()) {
df.delete();
}
String fileName = file.getOriginalFilename();
String[] strArr = fileName.split("\\.");
fileName = new Date().getTime() + "." + strArr[strArr.length - 1];
File targetFile = new File(rootPath + "file/userimg/app/", fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
UserInfoBean user = new UserInfoBean();
user.setUid(tmpuser.getUid());
user.setImgpath("file/userimg/app/" + fileName);
log.info("Action调用修改用户信息方法");
userService.updateUserInfo(user);
log.info("Action结束");
log.info("此方法耗费时间:" + (new Date().getTime() - st) + "毫秒");
return "redirect:userCenter";
}