1,UEditor的请求全部是通过ueditor.config.js文件的serverUrl变量"/config"接口调用的。在"/config"接口中判断action是否为uploadimage类型,表示为上传图片,在if块中写远程上传图片的代码。
本人这里使用的springcloud的feign远程调用,feign上传文件报FileUploadException: the request was rejected because no multipart boundary was found异常,请参考:https://blog.csdn.net/hlp4207/article/details/81235487。
使用httpclient的请参考:https://blog.csdn.net/lsqingfeng/article/details/90611686。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.ActionEnter;
import com.familylinkedu.common.support.JsonResult;
import com.familylinkedu.web.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
/**
* 百度富文本编辑器
*
* @author lixx
* @version 1.0
* @date 2020-05-06 14:18
*/
@RestController
public class UEditorController {
@Autowired
private UploadService uploadService;
@RequestMapping(value = "/config")
public void config(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
String action = request.getParameter("action");
if ("uploadimage".equals(action)) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartHttpServletRequest.getFile("file");
if(null == file || StringUtils.isBlank(file.getOriginalFilename())){
writer.write("{'state':'文件不能为空'}");
}
// 原来的文件名
String originalName = file.getOriginalFilename();
// 文件后缀
String suffix = originalName.substring(originalName.indexOf("."));
JsonResult jsonResult = uploadService.updateImage(file);
if (jsonResult.getCode().equals(0)) {
Map<String, String> resultMap = JSONObject.parseObject(JSONObject.toJSONString(jsonResult.getData()), Map.class);
Map<String, String> map = new HashMap<>(7);
map.put("state", resultMap.get("status"));
map.put("original", originalName);
map.put("size", resultMap.get("size"));
map.put("type", suffix);
map.put("url", resultMap.get("path"));
String json = JSON.toJSONString(map);
writer.write(json);
} else {
writer.write("{'state':'上传失败'}");
}
} else {
response.setContentType("application/json");
// rootPath路径: /D:/develop/web-service/target/classes/static/ueditor/jsp, 上传的图片会在这里路径下面
String rootPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/ueditor/jsp";
writer.write(new ActionEnter(request, rootPath).exec());
}
writer.flush();
writer.close();
}
}
返回的json格式请参考:
{
"state": "SUCCESS", // 成功
"original": "77426e62c700605da38a24f1dd495a55.jpg",// 原文件名
"size": "21382", // 文件大小
"title": "1589078472007085896.jpg", // 新文件名
"type": ".jpg", // 文件后缀
"url": "http://www.baidu.com/upload/image/20200510/1589078472007085896.jpg" // 文件路径
}
2,把config.json的imageUrlPrefix变量置空。
3,注意点上传图片接口的变量名称一定要与config.json文件中的imageFieldName变量值一致,不然会报Required request part '***' is not present异常。
4,看下运行效果。
5,SpringBoot整合UEditor请参考上一篇博客:https://blog.csdn.net/a1053765496/article/details/105965560 |