Springboot整合百度Ueditor

百度Ueditor的官网上只提供了JSP的版本,我这里用的是freemaker作为模板引擎。
https://ueditor.baidu.com/website/download.html
下载了源代码之后就把源码引入到项目中,如下图:

Springboot整合百度Ueditor1

把config.json拷贝到resources根路径下:

Springboot整合百度Ueditor2

添加一个UeditorController

Springboot整合百度Ueditor3

访问源码里提供的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

Springboot整合百度Ueditor4

此时会发现无法加载config.josn文件,需要修改ConfigManager类下的getConfigPath()方法:

Springboot整合百度Ueditor5

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文件:

Springboot整合百度Ueditor6

我这里是服务器的配置了,本地就只要存到某个盘符在的某个文件夹

修改上传文件类BinaryUploader:

Springboot整合百度Ueditor7

打开浏览器,你访问不到你刚刚上传的那张图片,通过调试就是404,需要在配置文件中进行目录的映射:

Springboot整合百度Ueditor8

到此,本地的调试功能基本完成了,但是最终我们是要打包部署到服务器的,我这里是jar包,mvn clean package打包

但是打包之后运行项目是会发现图片没法上传的,发现jar里无法以ClassLoader.getResource().getPath()获得的路径读取文件,得用Class类的getResourceAsStream()来读取

那就修改代码ConfigManager代码:

Springboot整合百度Ueditor9

String configContent = this.filter(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("config.json")));

至此就可以了。

补充几点吧,也是自己有时候会忘记:
在把程序部署打包部署到服务器的时候修改:

1.application 配置文件中把映射的路径更改一下

Springboot整合百度Ueditor10

2.config.json也是修改一下路径

Springboot整合百度Ueditor11

3.在引入资源的ueditor.config.js中修改一下serverUrl,因为我直接localhost部署到服务器,会初始化action失败,以至于会报错,无法使用,这个地址就是在UeditorController中的那个路径/config

Springboot整合百度Ueditor12

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值