UEditor百度富文本后端上传文件接口
直接上代码
接口:
@RequestMapping("/UEditorConfig")public String list(HttpServletRequest request, HttpServletResponse response) throws IOException {String config = environment.getProperty("ueditor.config.dir");String action = request.getParameter("action");String noCache = request.getParameter("noCache");if ("config".equals(action))return (new ActionEnter(request, config, "")).exec();else{String encode = request.getParameter("encode");MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;MultipartFile file = multipartRequest.getFile("upfile");String fileName = file.getOriginalFilename();String contentType = file.getContentType();long fileSize = file.getSize();String filePath = FileUploadUtils.upload(file);JSONObject jsonObject=new JSONObject();jsonObject.put("state","SUCCESS");jsonObject.put("original","myz.png");jsonObject.put("size", String.valueOf(fileSize));jsonObject.put("title",fileName);jsonObject.put("type",contentType);jsonObject.put("url",filePath);return jsonObject.toString();}}
其实就两种情况
一、读取配置文件
前端加载富文本时会请求一次富文本接口,而且上传文件时也是用的这个接口
if ("config".equals(action))return (new ActionEnter(request, config, "")).exec();
注意:ActionEnter是引入百度富文本的包
import com.baidu.ueditor.ActionEnter;
<dependency><groupId>com.gitee.qdbp.thirdparty</groupId><artifactId>ueditor</artifactId><version>1.4.3.3</version></dependency>
如果接口返回如下内容,说明你成功了,前端应该可以上传文件了(不会报后端配置错误)
二、上传文件
本质上就是返回一个json,自己构建就好了
else{String encode = request.getParameter("encode");MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;MultipartFile file = multipartRequest.getFile("upfile");String fileName = file.getOriginalFilename();String contentType = file.getContentType();long fileSize = file.getSize();String filePath = FileUploadUtils.upload(file);JSONObject jsonObject=new JSONObject();jsonObject.put("state","SUCCESS");jsonObject.put("original","myz.png");jsonObject.put("size", String.valueOf(fileSize));jsonObject.put("title",fileName);jsonObject.put("type",contentType);jsonObject.put("url",filePath);return jsonObject.toString();}
注意这里
HttpServletRequest里面的file要从 MultipartHttpServletRequest 拿出来
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("upfile");
FileUploadUtils.upload是我自己框架的上传文件方法,如果你的框架没有,可能需要自己写一个
String filePath = FileUploadUtils.upload(file);
搞定
ps:其实读取配置文件的接口本质上也是返回一个json,如果你愿意,也可以自己构建这个json,不引入百度的maven包