富文本编辑器Ueditor实战(三)-springboot集成

简介: 通过本文,您可了解springboot如何集成ueditor,如何自定义扩展后端的文件上传功能。

     前面的富文本编辑器Ueditor实战(一)和富文本编辑器Ueditor实战(二)-图片上传简单介绍了Ueditor能做啥以及Ueditor的简单配置。实际项目中,我们会结合具体的技术栈来进行扩展,本文以Springboot单体架构为例,前端使用thymeleaf集成ueditor。通过本文,您可了解springboot如何集成ueditor,如何自定义扩展后端的文件上传功能。

     环境说明:

     springboot2.2.13.RELEASE+jdk1.8+maven3.3.9+thymeleaf2.3.13+ueditor1.4.3

第一步、创建springboot项目以及pom.xml 配置

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.yelang</groupId><artifactId>boot-ueditor</artifactId><version>1.0.0</version><name>ueditor with springboot</name><description>ueditor集成springboot案例,集成图片上传等功能</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.13.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>2.0.0</version></dependency></dependencies></project>

系统配置文件说明:

# 项目相关配置system:# 名称  name: boot-ueditor
# 版本  version: 1.0.0# 文件路径 示例( Windows配置D:/boot-ueditor,Linux配置 /home/boot-ueditor)  profile: D:/boot-ueditor
server:  port: 8080spring:  thymeleaf:    cache: false    encoding: UTF-8
    mode: HTML
#prefix: classpath:/templates/#suffix: .html    servlet:      content-type: text/html
# 文件上传  servlet:     multipart:# 单个文件大小       max-file-size:  50MB
# 设置总上传的文件大小       max-request-size:  300MB

第二步、工程目录结构大致如下:

image.png

第三步、修改自定义的json配置,使用后台bean的方式进行配置。

首先定义一个普通的javaBean,如下:

image.png

在controller中定义ueditor配置文件的访问接口

@GetMapping(value="/opt",params="action=config")
@ResponseBodypublicObjectconfig() {
UeditorConfigVoconfig=newUeditorConfigVo();
StringurlPrefix=Constants.RESOURCE_PREFIX+"/upload/";
config.setImageUrlPrefix(urlPrefix);
config.setVideoUrlPrefix(urlPrefix);
config.setFileUrlPrefix(urlPrefix);
returnconfig;
    }

在这里,设置映射的静态资源目录为图片、视频、附件等资源的访问前缀,在文件上传成功后直接返回访问的前缀即可。

第四步、thymeleaf页面定义,引入相关的js资源

<!DOCTYPEhtml><htmllang="zh"xmlns:th="http://www.thymeleaf.org"><head><title>springboot整合thymeleaf和ueditor实例</title><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><scripttype="text/javascript"charset="utf-8"th:src="@{/ueditor1_4_3_3/ueditor.config.js?v=1.4.3.3}"></script><scripttype="text/javascript"charset="utf-8"th:src="@{/ueditor1_4_3_3/ueditor.all.js?v=1.4.3.3}"></script><!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败--><!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文--><scripttype="text/javascript"charset="utf-8"th:src="@{/ueditor1_4_3_3/lang/zh-cn/zh-cn.js?v=1.4.3.3}"></script><styletype="text/css">div {
width: 100%;
}
</style></head><body><div><scriptid="editor"type="text/plain"style="width: 100%; height: 500px;"></script></div><scriptth:inline="javascript">varurl= [[@{/ueditor/opt}]];
//实例化编辑器//建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例varue=UE.getEditor('editor',{serverUrl: url});
</script></body></html>

第五步、自定义后台文件上传接收器

@PostMapping(value="/opt",params="action=uploadimage")
@ResponseBodypublicMap<String,String>uploadImage(@RequestParam("upfile") MultipartFilefile) throwsIOException {
returnthis.fileProcess(file);
}
@PostMapping(value="/opt",params="action=uploadvideo")
@ResponseBodypublicMap<String,String>uploadVideo(@RequestParam("upfile") MultipartFilefile) throwsIOException {
returnthis.fileProcess(file);
}
@PostMapping(value="/opt",params="action=uploadfile")
@ResponseBodypublicMap<String,String>uploadFile(@RequestParam("upfile") MultipartFilefile) throwsIOException {
returnthis.fileProcess(file);
}
privateMap<String,String>fileProcess(MultipartFilefile) throwsFileSizeLimitExceededException, IOException{
Map<String,String>map=newHashMap<String, String>(4);
StringoriginalFilename=file.getOriginalFilename();
StringfileType=originalFilename.substring(originalFilename.lastIndexOf("."));
StringfileName=FileUploadUtils.upload(SysConfig.getUploadPath() +"/",file,fileType);
map.put("state", "SUCCESS");
map.put("url", fileName);
map.put("title", originalFilename);
map.put("original", originalFilename);
returnmap;
  }

上述代码采用params参数来区分不同的请求,前台也可以设置后台指定一个固定的上传接口即可(可参考官方的说明)。

       通过以上的实例可以完成自定义的图片、附件等上传的功能,并且可以在前台看到上传的资源在富文本编辑器中。

ps:有兴趣的朋友可以试下mp4资源的上传是什么效果。怎么解决mp4上传后的问题,后续会再出一篇博文进行详细的介绍,欢迎关注。

目录
相关文章
|
7月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1160 3
|
6月前
|
人工智能 自然语言处理 API
快速集成GPT-4o:下一代多模态AI实战指南
快速集成GPT-4o:下一代多模态AI实战指南
536 101
|
5月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
1044 2
Spring Boot 3.x 微服务架构实战指南
|
6月前
|
消息中间件 Ubuntu Java
SpringBoot整合MQTT实战:基于EMQX实现双向设备通信
本教程指导在Ubuntu上部署EMQX 5.9.0并集成Spring Boot实现MQTT双向通信,涵盖服务器搭建、客户端配置及生产实践,助您快速构建企业级物联网消息系统。
2391 1
|
6月前
|
人工智能 Java API
Java与大模型集成实战:构建智能Java应用的新范式
随着大型语言模型(LLM)的API化,将其强大的自然语言处理能力集成到现有Java应用中已成为提升应用智能水平的关键路径。本文旨在为Java开发者提供一份实用的集成指南。我们将深入探讨如何使用Spring Boot 3框架,通过HTTP客户端与OpenAI GPT(或兼容API)进行高效、安全的交互。内容涵盖项目依赖配置、异步非阻塞的API调用、请求与响应的结构化处理、异常管理以及一些面向生产环境的最佳实践,并附带完整的代码示例,助您快速将AI能力融入Java生态。
1030 12
|
8月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
521 3
|
8月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
765 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
8月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
821 2
|
8月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
467 2