百度Ueditor的官网上只提供了JSP的版本,我这里用的是freemaker作为模板引擎。
https://ueditor.baidu.com/website/download.html
下载了源代码之后就把源码引入到项目中,如下图:
把config.json拷贝到resources根路径下:
添加一个UeditorController
访问源码里提供的index,如果能够正常访问那就说明源码拷贝成功了,这个时候是不能够上传图片的,还需要引入外部依赖
在Pom中引入
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
然后再照着源码中的编写UeditorController
此时会发现无法加载config.josn文件,需要修改ConfigManager类下的getConfigPath()方法:
this.getClass().getClassLoader().getResource(“config.json”).toURI().getPath();
此处需要先转为URI再getPath(),否则如果你的项目路径带空格或者带中文则无法读取到文件
编写完成之后,就可以运行项目了:http://localhost:8080/config?action=config
这时候就会读取到JSON数据了
此时如果上传图片还是不行的,需要在BinaryUploader中修改,网上的资料是说springMVC框架对字节流的request进行了处理,所以获取不到字节流,此时需要采用multipartResolver:
public class BinaryUploader {
public static final State save(HttpServletRequest request,
Map<String, Object> conf) {
// FileItemStream fileStream = null;
// boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;
if (!ServletFileUpload.isMultipartContent(request)) {
return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
}
// ServletFileUpload upload = new ServletFileUpload(
// new DiskFileItemFactory());
//
// if ( isAjaxUpload ) {
// upload.setHeaderEncoding( "UTF-8" );
// }
try {
// FileItemIterator iterator = upload.getItemIterator(request);
//
// while (iterator.hasNext()) {
// fileStream = iterator.next();
//
// if (!fileStream.isFormField())
// break;
// fileStream = null;
// }
//
// if (fileStream == null) {
// return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
// }
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile(conf.get("fieldName").toString());
if (multipartFile == null) {
return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
}
String savePath = (String) conf.get("savePath");
// String originFileName = fileStream.getName();
String originFileName = multipartFile.getOriginalFilename();
String suffix = FileType.getSuffixByFilename(originFileName);
originFileName = originFileName.substring(0,
originFileName.length() - suffix.length());
savePath = savePath + suffix;
long maxSize = ((Long) conf.get("maxSize")).longValue();
if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
}
savePath = PathFormat.parse(savePath, originFileName);
// String physicalPath = (String) conf.get("rootPath") + savePath;
String basePath = (String) conf.get("basePath");
String physicalPath = basePath + savePath;
// InputStream is = fileStream.openStream();
InputStream is = multipartFile.getInputStream();
State storageState = com.baidu.ueditor.upload.StorageManager.saveFileByInputStream(is,
physicalPath, maxSize);
is.close();
if (storageState.isSuccess()) {
storageState.putInfo("url", PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
}
return storageState;
}
// catch (FileUploadException e) {
// return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
// }
catch (IOException e) {
}
return new BaseState(false, AppInfo.IO_ERROR);
}
private static boolean validType(String type, String[] allowTypes) {
List<String> list = Arrays.asList(allowTypes);
return list.contains(type);
}
}
此时图片已经能够上传了
如果没有修改存储图片的路径,图片只是存储在了tomcat的缓存中,只要重启了tomcat就会被删除,这时候需要修改config.json文件:
我这里是服务器的配置了,本地就只要存到某个盘符在的某个文件夹
修改上传文件类BinaryUploader:
打开浏览器,你访问不到你刚刚上传的那张图片,通过调试就是404,需要在配置文件中进行目录的映射:
到此,本地的调试功能基本完成了,但是最终我们是要打包部署到服务器的,我这里是jar包,mvn clean package打包
但是打包之后运行项目是会发现图片没法上传的,发现jar里无法以ClassLoader.getResource().getPath()获得的路径读取文件,得用Class类的getResourceAsStream()来读取
那就修改代码ConfigManager代码:
String configContent = this.filter(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("config.json")));
至此就可以了。
补充几点吧,也是自己有时候会忘记:
在把程序部署打包部署到服务器的时候修改:
1.application 配置文件中把映射的路径更改一下
2.config.json也是修改一下路径
3.在引入资源的ueditor.config.js中修改一下serverUrl,因为我直接localhost部署到服务器,会初始化action失败,以至于会报错,无法使用,这个地址就是在UeditorController中的那个路径/config