/**
* 图片上传
* @return
*/
@PostMapping(value = {"/uploadPicture","/uploadHead"})
public Result uploadHeadPortrait(HttpSession session,@RequestParam("file") MultipartFile file,HttpServletRequest request){
User user = (User)session.getAttribute("user");
if(null == user){
return Result.error("no user");
}
if (file.getOriginalFilename().isEmpty()){
return Result.error("file name error");
}
try {
FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/fdfs.conf");
//获取文件名
String originalFilename = file.getOriginalFilename();
//获取文件后缀名
String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
//拼接图片服务器路径
String filename = IMAGE_SERVER_URL + fastDFSClient.uploadFile(file.getBytes(), extName);
//获取请求路径
String url = request.getRequestURI().trim();
if(url.equals("/uploadPicture")){
//保存相册
Picture picture = new Picture();
picture.setUser(user);
picture.setCreateTime(new Date());
picture.setPath(filename);
pictureService.save(picture);
return Result.success();
}else if(url.equals("/uploadHead")){
//保存用户头像
user.setHeadPortrait(filename);
userService.upload(user);
return Result.success();
}
return Result.error("url error");
}catch (Exception e){
e.printStackTrace();
return Result.error("picture upload failed");
}
}