1.和上一篇的SpringMVC上传文件代码和工具相差无几,有的只是Controller最后调用的时候需要根据富文本上传文件的格式改一下就好
2.Controller
@RequestMapping("richtext_img_upload.do")
@ResponseBody
public Map richtextImgUpload(HttpSession session, @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
Map resultMap = Maps.newHashMap();
User user = (User) session.getAttribute(Const.CURRENT_USER);
if (user == null) {
resultMap.put("success", false);
resultMap.put("msg", "请登录管理员");
return resultMap;
}
//富文本中对于返回值有自己的要求,我们使用是simditor所以按照simditor的要求进行返回
// {
// "success": true/false,
// "msg": "error message", # optional
// "file_path": "[real file path]"
// }
if (iUserService.checkAdminRole(user).isSuccess()) {
String path = request.getSession().getServletContext().getRealPath("upload");
String targetFileName = iFileService.upload(file, path);
if (StringUtils.isBlank(targetFileName)) {
resultMap.put("success", false);
resultMap.put("msg", "上传失败");
return resultMap;
}
String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFileName;
resultMap.put("success", true);
resultMap.put("msg", "上传成功");
resultMap.put("file_path", url);
response.addHeader("Access-Control-Allow-Headers", "X-File-Name");
return resultMap;
} else {
resultMap.put("success", false);
resultMap.put("msg", "无权限操作");
return resultMap;
}
}
3.前台JSP