/** * app 更新头像 * * @param base64Code * @param origName * @param req * @param res * @param indexImgSize * @throws Exception * @author linwk 2016年6月13日 */ @RequestMapping(value = "/uploadImg.htm", method = RequestMethod.POST) public void uploadFrontByBase64(String base64Code, HttpServletRequest req, HttpServletResponse res, Integer indexImgSize) throws Exception { try { if (StringUtils.isBlank(base64Code)) { throw new BusinessException("图片不能为空!"); } Long userId = RequestUtils.getCurrentUserId(req); UserVO user = userService.getUserVOById(userId);
// boolean a = GenerateImage(base64Code);
// 商品图片存放路径
String storageFilePath;
String storageFileName;
String base64Img = null;
// 获取图片后缀
String imgSuffix = "jpg";
String pattern = "data:image/(.*?);base64";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(base64Code);
ArrayList<String> strs = Lists.newArrayList();
if (m.find()) {
strs.add(m.group(1));
}
for (String s : strs) {
imgSuffix = s;
}
if (base64Code.indexOf("base64,") > 0) {
String[] base64LiStrings = base64Code.split("base64,");
// 获取图片内容
base64Img = base64LiStrings[1];
} else {
base64Img = base64Code;
}
storageFilePath = "user/" + user.getId() + "/" + DateUtil.getNowDateToString("yyyy") + "/" + DateUtil.getNowDateToString("MM") + "/"
+ DateUtil.getNowDateToString("dd");
storageFileName = UUID.randomUUID().toString();
storageFileName += "." + imgSuffix;
// 原图id
Long oldImgId = user.getPhotoId();
// 更新个人头像(只允许传一张)
List<Accessory> fileList = new ArrayList<Accessory>(0);
FileUploadVO fileUpload = new FileUploadVO();
BASE64Decoder decoder = new BASE64Decoder();
final byte[] decoderBytes = decoder.decodeBuffer(base64Img);
MultipartFile file = new MultipartFile() {
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public long getSize() {
return 0;
}
@Override
public String getOriginalFilename() {
return "";
}
@Override
public String getName() {
return "";
}
@Override
public InputStream getInputStream() throws IOException {
return null;
}
@Override
public String getContentType() {
return null;
}
@Override
public byte[] getBytes() throws IOException {
return decoderBytes;
}
};
fileUpload.setFile(file);
fileUpload.setStorageFilePath(storageFilePath);
fileUpload.setStorageFileName(storageFileName);
// fileUpload.setUserId(user.getId());
// 上传文件到硬盘
Accessory acc = UploadFileUtil.uploadFile(fileUpload);
// 保存文件到数据库
acc = fileService.insertFile(acc);
fileList.add(acc);
// 更新用户
user.setPhotoId(acc.getId());
userService.updateUser(user);
// 从数据库删除原文件
fileService.deleteFile(oldImgId);
// 从硬盘删除原文件
UploadFileUtil.deleteFile(fileService.getById(oldImgId));
ResponseOutputUtils.renderJson(res, MessageDTO.newInstance(true,"上传成功", null));
} catch (BusinessException ex) {
ResponseOutputUtils.renderJson(res, MessageDTO.newInstance(false, ex.getMessage(), null));
} catch (Exception ex) {
logger.error("上传头像异常", ex);
ResponseOutputUtils.renderJson(res, MessageDTO.newInstance(false, "上传头像异常", null));
}
}