版权声明: https://blog.csdn.net/qq_20544709/article/details/81279784
1、问题原因:
由于百度编辑器相关jar包里面的class文件在某些特殊的情况下无法完全引入,或者映入过后无法读取config.json的配置文件导致上传的相关配置不正确。
2、解决办法:
修改ueditor.config.js的配置读取的控制器层接口地址(注:该地址用于返回上传的相关配置josn和上传文件)
控制器代码示例.(注,自己根据自己需要的配置或者上传文件保存地址做相应代码调整)
package egov.portal.mapi.ueditor.controller;
import com.alibaba.fastjson.JSONObject;
import egov.common.base.controller.BaseController;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
@RequestMapping(value="/ueditor")
public class EeditorController extends BaseController {
@RequestMapping(value = "config")
@ResponseBody
public JSONObject config(String action, @RequestParam(required = false) MultipartFile upfile, @RequestParam(required = false) String callback, @RequestParam(required = false) String encode,
HttpServletRequest request) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject = getConfig();
// action参数为getConfig中的jsonObject.put("imageActionName", "uploadimage");
if (upfile!=null) {
// 此处返回上传后的图片路径,json格式为{["url":"http://xinrui.com/image/1.png","state":"SUCCESS"]}
try {
String fileName = upfile.getOriginalFilename();
String baseDir = request.getServletContext().getRealPath("/");
// 设置附件访问地址和存放路径
// 相对路径 attach/uploadfiles/1/files/2016/07/201607281232.jpg
String filePath = "/file/img/"+fileName;
// 保存上传的文件
String networkProtocol = request.getScheme();
//网络IP
String ip = request.getServerName();
//端口号
int port = request.getServerPort();
//项目发布路径
String webApp = request.getContextPath();
File saveFile = new File(baseDir, filePath);
jsonObject.put("url",networkProtocol+"://"+ip+":"+port+"/"+webApp+filePath);
FileUtils.copyInputStreamToFile(upfile.getInputStream(), saveFile);
jsonObject.put("state","SUCCESS");
} catch (IOException e) {
// TODO Auto-generated catch block
jsonObject.put("state","ERROR");
}
//jsonObject = imgcompressService.ueEditorUpload(request);
return jsonObject;
}
return jsonObject;
}
public JSONObject getConfig() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("imageActionName", "uploadimage"); // 执行上传图片的action名称
jsonObject.put("imageAllowFiles", new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" }); // 允许上传的图片类型
jsonObject.put("imageFieldName", "upfile"); // 提交的图片表单名称
jsonObject.put("imageMaxSize", "2048000"); // 上传大小限制,单位B
jsonObject.put("imageCompressEnable", true); // 是否压缩图片,默认是true
jsonObject.put("imageCompressBorder", 1600); // 图片压缩最长边限制
jsonObject.put("imageInsertAlign", "none"); // 插入的图片浮动方式
jsonObject.put("imageUrlPrefix", ""); // 图片访问路径前缀
jsonObject.put("imagePathFormat", "/{yyyy}{mm}{dd}/{time}{rand:6}"); // 上传保存路径,可以自定义保存路径和文件名格式
jsonObject.put("fileActionName", "uploadfile");/* controller里,执行上传视频的action名称 */
jsonObject.put("fileFieldName","upfile"); /* 提交的文件表单名称 */
jsonObject.put("filePathFormat", "/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}"); /* 上传保存路径,可以自定义保存路径和文件名格式 */
jsonObject.put("fileUrlPrefix", ""); /* 文件访问路径前缀 */
jsonObject.put("fileMaxSize", 51200000);/* 上传大小限制,单位B,默认50MB */
jsonObject.put("fileAllowFiles",new String[]{
".png", ".jpg", ".jpeg", ".gif", ".bmp",
".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
});
return jsonObject;
}
}
注意这句代码
jsonObject.put("imageFieldName", "upfile"); // 提交的图片表单名称
upfile:表示上传后文件对象的变量名称
上传路径没必要配置,我这里不做删除,当然这里的配置实在代码里写死,也可通过读取配置文件进行读取配置,这里就不多做介绍