@RequestMapping(value=”/uploadphoto”,method=RequestMethod.POST)
@ResponseBody
public String uploadPhoto(HttpServletRequest request){
…
}
@RequestMapping(value="/uploadphoto",method=RequestMethod.POST)
@ResponseBody
public String uploadPhoto(HttpServletRequest request){
AppResultJson resultJson = new AppResultJson();
//创建一个通用的多部分解析器.
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//设置编码
commonsMultipartResolver.setDefaultEncoding("utf-8");
//判断 request 是否有文件上传,即多部分请求..
if (commonsMultipartResolver.isMultipart(request)){
try {
MultipartHttpServletRequest mulReq = (MultipartHttpServletRequest) request;
List<MultipartFile> files=mulReq.getFiles("files");
List<String> map=relieveRiskService.uploadPhoto(files);
resultJson.setObj(map);
resultJson.setMsg("上传成功");
resultJson.setStatus(ResultStatusConstants.SUCCESS);
} catch (Exception e) {
resultJson.setMsg("上传失败,"+e.getMessage());
resultJson.setStatus(ResultStatusConstants.SYS_ERROR);
logger.error(e.getMessage());
}
}else{
resultJson.setMsg("上传照片不能为空。");
resultJson.setStatus(ResultStatusConstants.PARAM_ERROR);
}
return JsonMapper.toJsonString(resultJson);
}