@RequestMapping(value = "/api/faceData/uploadFileTest", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST})
@ResponseBody
public Map<String, Object> savePic(@RequestBody JSONObject params) throws Exception {
String base64Pic = "";
Map<String, Object> resultMap = new HashMap<String, Object>();
base64Pic = params.getString("file_base64");
if (base64Pic == null) { // 图像数据为空
resultMap.put("resultCode", 0);
resultMap.put("msg", "图片为空");
} else {
BASE64Decoder decoder = new BASE64Decoder();
String baseValue = base64Pic.replaceAll(" ", "+");//前台在用Ajax传base64值的时候会把base64中的+换成空格,所以需要替换回来。
byte[] b = decoder.decodeBuffer(baseValue.replace("data:image/jpeg;base64,", ""));//去除base64中无用的部分
base64Pic = base64Pic.replace("base64,", "");
System.out.println(b);
String imgFilePath = "C:\\Users\\ron\\Desktop\\2.jpg";
try {
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
File file = new File(imgFilePath);
// 如果要返回file文件这边return就可以了
System.err.println("file.getName = " + file.getName());
System.err.println("file.getPath = " + file.getPath());
OutputStream out = new FileOutputStream(file.getPath());
out.write(b);
out.flush();
out.close();
resultMap.put("resultCode", 1);
resultMap.put("msg", "存储成功");
} catch (Exception e) {
resultMap.put("resultCode", 0);
resultMap.put("msg", "存储异常");
}
}
return resultMap;
}
|