第一种:前台将文件转换成BASE64的字符串,后台接收BASE64的字符串并解码:
这种方式有一个问题,文件比较大的时候,后台可能会接收不了,推荐使用第二种方式。
@SuppressWarnings("restriction")
@RequestMapping(value = "/uploadImageBase64", method = RequestMethod.POST)
public ResponseEntity uploadImageBase(@RequestParam(value = "img") String base64Data,
@RequestParam String token){
log.info("==上传图片==");
log.info("==接收到的数据=="+base64Data);
JSONObject json = new JSONObject();
String dataPrix = ""; //base64格式前头
String data = "";//实体部分数据
if(base64Data==null||"".equals(base64Data)){
json.put("STATUS", "ERROR");
json.put("MSG", "上传失败,上传图片数据为空");
return new ResponseEntity(json, HttpStatus.BAD_REQUEST);
}else {
String [] d = base64Data.split("base64,");//将字符串分成数组
if(d != null && d.length == 2){
dataPrix = d[0];
data = d[1];
}else {
json.put("STATUS", "ERROR");
json.put("MSG", "上传失败,数据不合法");
return new ResponseEntity(json, HttpStatus.BAD_REQUEST);
}
}
String suffix = "";//图片后缀,用以识别哪种格式数据
//data:image/jpeg;base64,base64编码的jpeg图片数据
if("data:image/jpeg;".equalsIgnoreCase(dataPrix)){
suffix = ".jpg";
}else if("data:image/x-icon;".equalsIgnoreCase(dataPrix)){
//data:image/x-icon;base64,base64编码的icon图片数据
suffix = ".ico";
}else if("data:image/gif;".equalsIgnoreCase(dataPrix)){
//data:image/gif;base64,base64编码的gif图片数据
suffix = ".gif";
}else if("data:image/png;".equalsIgnoreCase(dataPrix)){
//data:image/png;base64,base64编码的png图片数据
suffix = ".png";
}else {
json.put("STATUS", "ERROR");
json.put("MSG", "上传图片格式不合法");
return new ResponseEntity(json, HttpStatus.BAD_REQUEST);
}
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
String tempFileName=uuid+suffix;
String imgFilePath = "D:\\logs\\"+tempFileName;//新生成的图片
BASE64Decoder decoder = new BASE64Decoder();
try {
//Base64解码
//byte[] b = Base64.getDecoder().decode(data);
byte[] b = decoder.decodeBuffer(data);
for(int i=0;i(json, HttpStatus.OK);
} catch (IOException e) {
log.error("系统异常,上传图片失败,msg={}",e.getMessage());
json.put("STATUS", "ERROR");
json.put("MSG", "系统异常,上传图片失败");
return new ResponseEntity(json, HttpStatus.INTERNAL_SERVER_ERROR);//500,系统异常
}
}
上面的BASE64Decoder为: sun.misc.BASE64Decoder,直接import即可
第二种:前台以文件格式传输,后台接收MultipartFile 类型的文件:
@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
public ResponseEntity uploadImageFile(@RequestParam("img") MultipartFile uploadImage,
@RequestParam String token,
@RequestParam String type){
JSONObject json = new JSONObject();
try {
if(uploadImage==null){
json.put("STATUS", "ERROR");
json.put("MSG", "上传失败,上传图片数据为空");
return new ResponseEntity(json, HttpStatus.BAD_REQUEST);
}
String suffix = uploadImage.getContentType().toLowerCase();//图片后缀,用以识别哪种格式数据
suffix = suffix.substring(suffix.lastIndexOf("/")+1);
if(suffix.equals("jpg") || suffix.equals("jpeg") || suffix.equals("png") || suffix.equals("gif")) {
String fileName = type + "_" + UUID.randomUUID().toString().replaceAll("-", "") + "." + suffix;
String imgFilePath = "D:\\logs\\";//新生成的图片
File targetFile = new File(imgFilePath, fileName);
if(!targetFile.getParentFile().exists()){ //注意,判断父级路径是否存在
targetFile.getParentFile().mkdirs();
}
//保存
uploadImage.transferTo(targetFile);
json.put("STATUS", "200");
json.put("MSG", "上传图片成功");
return new ResponseEntity(json, HttpStatus.OK);
}
log.error("系统异常,上传图片格式非法");
json.put("STATUS", "ERROR");
json.put("MSG", "上传图片格式非法");
return new ResponseEntity(json, HttpStatus.BAD_REQUEST);//500,系统异常
} catch (Exception e) {
log.error("系统异常,上传图片失败,msg={}",e.getMessage());
json.put("STATUS", "ERROR");
json.put("MSG", "系统异常,上传图片失败");
return new ResponseEntity(json, HttpStatus.INTERNAL_SERVER_ERROR);//500,系统异常
}
}