base64位图片上传

前端图片上传
//@param imageFile
// @return

@RequestMapping(value = “/base64-upload”, method = RequestMethod.POST)
public ResponseMessage base64Upload(String imageFile) {
ResponseMessage responseMessage = new ResponseMessage(100000, “成功”);
// 通过base64来转化图片
imageFile = imageFile.replaceAll(“data:image/jpeg;base64,”, “”);
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] imageByte = null;
try {
imageByte = decoder.decodeBuffer(imageFile);
for (int i = 0; i < imageByte.length; ++i) {
// 调整异常数据
if (imageByte[i] < 0) {
imageByte[i] += 256;
}
}
} catch (Exception e) {
e.printStackTrace();
}
// 生成文件名
String files = new SimpleDateFormat(“yyyyMMddHHmmssSSS”)
.format(new Date())
+ (new Random().nextInt(9000) % (9000 - 1000 + 1) + 1000)
+ “.png”;
// 生成文件路径
String filename = System.getProperty(“user.dir”) + File.separator + files;
try {
// 生成文件
File fileile = new File(filename);
fileile.createNewFile();
if (!fileile.exists()) {
fileile.createNewFile();
}
OutputStream imageStream = new FileOutputStream(imageFile);
imageStream.write(imageByte);
imageStream.flush();
imageStream.close();
StorageEntity store = storageService.store(getBytes(fileile), “jpg”);
responseMessage.setData(store.getUrl());
} catch (Exception e) {
e.printStackTrace();
}finally {
File file=new File(filename);
if(file.exists()){
file.delete();
}
}
return responseMessage;
}

你可能感兴趣的:(文件上传)