当前位置:   article > 正文

Spring整合UEditor富文本编辑器,并上传图片(最全教程)_ueditor 上传图片 spring

ueditor 上传图片 spring

目录

 

前戏

创建项目

下载UEditor的JSP版本

在服务器端进行编码配置

在前端页面使用并配置UEditor编辑器

实现上传图片的功能(上传附件、文件等同理)


前戏

博主最近在做的项目中,有一个发布新闻的需求,所以用到了富文本编辑器,经过前端人员的对比分析,最终采用了百度开源的UEditor编辑器。

但是这个事情并不是前端一个部门的事,这是需要前后端配合的,因为涉及到上传图片、文件或附件,一开始踩了很多坑,网友们的意见也是参差不齐,现在终于理清了,完美整合到了项目中,在这里做一下记录,希望帮助到需要的人。

本文章中的示例,采用SSM实现(当然这里没有MyBatis),因为它需要下载UEditor提供的一些JSP版本的文件夹,这些在Spring Boot中配置起来是不划算的,而且我觉得Spring Boot这玩意儿,应该只用来开发分布式或者说微服务的API接口,涉及到页面的东西最好不要采用它。

创建项目

采用maven工具创建,具体的流程不说了,想必各位创建一个maven项目还是不在话下的,只贴一下结构:

其中UEController.java、home.html我们先让它空着,其他文件的内容:

PageController.java:

  1. package com.blog.ueditor.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. /**
  5. * 页面转发器
  6. * @author 秋枫艳梦
  7. * @date 2019-06-21
  8. * */
  9. @Controller
  10. public class PageController {
  11. /**
  12. * 跳到富文本编辑页面
  13. * @return 视图名称
  14. *
  15. * */
  16. @RequestMapping(value = "/home.html")
  17. public String goHome(){
  18. return "home";
  19. }
  20. }

application-context.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context.xsd
  16. ">
  17. <context:component-scan base-package="com.blog.ueditor.controller"/>
  18. <context:annotation-config />
  19. </beans>

application-servlet.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  13. ">
  14. <import resource="application-context.xml"/> <!--引入另一篇配置文件-->
  15. <mvc:resources mapping="/statics/**" location="/statics/"/>
  16. <!--完成请求与处理器之间的映射,并处理JSON数据格式的转码-->
  17. <mvc:annotation-driven>
  18. <mvc:message-converters>
  19. <!--处理时间格式-->
  20. <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
  21. <property name="supportedMediaTypes">
  22. <list>
  23. <value>text/html;charset=UTF-8</value>
  24. <value>application/json;charset=UTF-8</value>
  25. </list>
  26. </property>
  27. <property name="features">
  28. <list>
  29. <value>WriteDateUseDateFormat</value>
  30. </list>
  31. </property>
  32. </bean>
  33. </mvc:message-converters>
  34. </mvc:annotation-driven>
  35. <!--配置视图解析器-->
  36. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  37. <property name="prefix" value="/"/>
  38. <property name="suffix" value=".jsp"/>
  39. </bean>
  40. <!-- html视图解析器 --> <!-- 必须先配置freemarkerConfig,注意html是没有prefix前缀属性的 -->
  41. <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  42. <property name="templateLoaderPath">
  43. <value>/html/</value>
  44. </property>
  45. <property name="freemarkerSettings">
  46. <props>
  47. <prop key="default_encoding">UTF-8</prop>
  48. </props>
  49. </property>
  50. </bean>
  51. <bean id="htmlviewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
  52. <property name="suffix" value=".html"/>
  53. <property name="order" value="0"/>
  54. <property name="contentType" value="text/html;charset=UTF-8"/>
  55. </bean>
  56. <!--配置MultipartResolver,上传文件-->
  57. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  58. <property name="maxUploadSize" value="5000000"/>
  59. <property name="defaultEncoding" value="UTF-8"/>
  60. </bean>
  61. </beans>

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <display-name>Archetype Created Web Application</display-name>
  7. <!--定义核心Servlet,MVC的入口-->
  8. <servlet>
  9. <servlet-name>DispatcherServlet</servlet-name>
  10. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  11. <init-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:application-*.xml</param-value>
  14. </init-param>
  15. <load-on-startup>1</load-on-startup>
  16. </servlet>
  17. <servlet-mapping>
  18. <servlet-name>DispatcherServlet</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21. <servlet-mapping>
  22. <servlet-name>DispatcherServlet</servlet-name>
  23. <url-pattern>/home.html</url-pattern>
  24. </servlet-mapping>
  25. <!--定义Spring字节编码过滤器-->
  26. <filter>
  27. <filter-name>encodingFilter</filter-name>
  28. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  29. <init-param>
  30. <param-name>encoding</param-name>
  31. <param-value>UTF-8</param-value>
  32. </init-param>
  33. <init-param>
  34. <param-name>forceEncoding</param-name>
  35. <param-value>true</param-value>
  36. </init-param>
  37. </filter>
  38. <filter-mapping>
  39. <filter-name>encodingFilter</filter-name>
  40. <url-pattern>/*</url-pattern>
  41. </filter-mapping>
  42. <!--配置环境参数,指定Spring配置文件所在的位置-->
  43. <context-param>
  44. <param-name>contextConfigLocation</param-name>
  45. <param-value>classpath:application-*.xml</param-value>
  46. </context-param>
  47. <!--定义监听器,Web容器一启动,就启动Spring容器-->
  48. <listener>
  49. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  50. </listener>
  51. <welcome-file-list>
  52. <welcome-file>home.html</welcome-file>
  53. </welcome-file-list>
  54. </web-app>

好了,到这里,我们的环境已经准备好了,接下来让我们下载UE编辑器并进行必要的配置。

下载UEditor的JSP版本

UEditor下载地址传送门

下载完成之后解压出来,是一个utf8-jsp文件夹,复制到项目的根目录,即webapps下:

 然后我们把以下jar包添加到自己的pom.xml中:

贴一下最终的pom.xml(以我的为例):

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.blog.ueditor</groupId>
  6. <artifactId>demo</artifactId>
  7. <version>1.0</version>
  8. <packaging>war</packaging>
  9. <name>demo Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.7</maven.compiler.source>
  15. <maven.compiler.target>1.7</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>4.11</version>
  22. <scope>test</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>javax.annotation</groupId>
  26. <artifactId>javax.annotation-api</artifactId>
  27. <version>1.3.1</version>
  28. </dependency>
  29. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-context-support</artifactId>
  33. <version>5.1.5.RELEASE</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.freemarker</groupId>
  37. <artifactId>freemarker</artifactId>
  38. <version>2.3.28</version>
  39. </dependency>
  40. <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
  41. <dependency>
  42. <groupId>javax.servlet</groupId>
  43. <artifactId>jstl</artifactId>
  44. <version>1.2</version>
  45. </dependency>
  46. <!-- https://mvnrepository.com/artifact/taglibs/standard -->
  47. <dependency>
  48. <groupId>taglibs</groupId>
  49. <artifactId>standard</artifactId>
  50. <version>1.1.2</version>
  51. </dependency>
  52. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  53. <dependency>
  54. <groupId>javax.servlet</groupId>
  55. <artifactId>javax.servlet-api</artifactId>
  56. <version>3.1.0</version>
  57. <scope>provided</scope>
  58. </dependency>
  59. <dependency>
  60. <groupId>junit</groupId>
  61. <artifactId>junit</artifactId>
  62. <version>4.11</version>
  63. <scope>test</scope>
  64. </dependency>
  65. <!--Spring模块-->
  66. <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
  67. <dependency>
  68. <groupId>org.springframework</groupId>
  69. <artifactId>spring-aop</artifactId>
  70. <version>5.1.3.RELEASE</version>
  71. </dependency>
  72. <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
  73. <dependency>
  74. <groupId>org.springframework</groupId>
  75. <artifactId>spring-aspects</artifactId>
  76. <version>5.1.3.RELEASE</version>
  77. </dependency>
  78. <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
  79. <dependency>
  80. <groupId>org.springframework</groupId>
  81. <artifactId>spring-beans</artifactId>
  82. <version>5.1.3.RELEASE</version>
  83. </dependency>
  84. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  85. <dependency>
  86. <groupId>org.springframework</groupId>
  87. <artifactId>spring-context</artifactId>
  88. <version>5.1.3.RELEASE</version>
  89. </dependency>
  90. <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  91. <dependency>
  92. <groupId>org.springframework</groupId>
  93. <artifactId>spring-core</artifactId>
  94. <version>5.1.3.RELEASE</version>
  95. </dependency>
  96. <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
  97. <dependency>
  98. <groupId>org.springframework</groupId>
  99. <artifactId>spring-expression</artifactId>
  100. <version>5.1.3.RELEASE</version>
  101. </dependency>
  102. <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument -->
  103. <dependency>
  104. <groupId>org.springframework</groupId>
  105. <artifactId>spring-instrument</artifactId>
  106. <version>5.1.3.RELEASE</version>
  107. </dependency>
  108. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  109. <dependency>
  110. <groupId>org.springframework</groupId>
  111. <artifactId>spring-jdbc</artifactId>
  112. <version>5.1.3.RELEASE</version>
  113. </dependency>
  114. <!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
  115. <dependency>
  116. <groupId>org.springframework</groupId>
  117. <artifactId>spring-messaging</artifactId>
  118. <version>5.1.3.RELEASE</version>
  119. </dependency>
  120. <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
  121. <dependency>
  122. <groupId>org.springframework</groupId>
  123. <artifactId>spring-test</artifactId>
  124. <version>5.1.3.RELEASE</version>
  125. <scope>test</scope>
  126. </dependency>
  127. <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
  128. <dependency>
  129. <groupId>org.springframework</groupId>
  130. <artifactId>spring-tx</artifactId>
  131. <version>5.1.3.RELEASE</version>
  132. </dependency>
  133. <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
  134. <dependency>
  135. <groupId>org.springframework</groupId>
  136. <artifactId>spring-web</artifactId>
  137. <version>5.1.3.RELEASE</version>
  138. </dependency>
  139. <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  140. <dependency>
  141. <groupId>org.springframework</groupId>
  142. <artifactId>spring-webmvc</artifactId>
  143. <version>5.1.3.RELEASE</version>
  144. </dependency>
  145. <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
  146. <dependency>
  147. <groupId>aopalliance</groupId>
  148. <artifactId>aopalliance</artifactId>
  149. <version>1.0</version>
  150. </dependency>
  151. <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
  152. <dependency>
  153. <groupId>org.aspectj</groupId>
  154. <artifactId>aspectjweaver</artifactId>
  155. <version>1.9.2</version>
  156. </dependency>
  157. <!-- https://mvnrepository.com/artifact/log4j/log4j -->
  158. <dependency>
  159. <groupId>log4j</groupId>
  160. <artifactId>log4j</artifactId>
  161. <version>1.2.17</version>
  162. </dependency>
  163. <!--文件模块-->
  164. <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
  165. <dependency>
  166. <groupId>commons-io</groupId>
  167. <artifactId>commons-io</artifactId>
  168. <version>2.6</version>
  169. </dependency>
  170. <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  171. <dependency>
  172. <groupId>commons-logging</groupId>
  173. <artifactId>commons-logging</artifactId>
  174. <version>1.2</version>
  175. </dependency>
  176. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
  177. <dependency>
  178. <groupId>org.apache.commons</groupId>
  179. <artifactId>commons-lang3</artifactId>
  180. <version>3.8.1</version>
  181. </dependency>
  182. <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
  183. <dependency>
  184. <groupId>commons-fileupload</groupId>
  185. <artifactId>commons-fileupload</artifactId>
  186. <version>1.3.3</version>
  187. </dependency>
  188. <!-- https://mvnrepository.com/artifact/com.gitee.qdbp.thirdparty/ueditor -->
  189. <dependency>
  190. <groupId>com.gitee.qdbp.thirdparty</groupId>
  191. <artifactId>ueditor</artifactId>
  192. <version>1.4.3.3</version>
  193. </dependency>
  194. <!-- https://mvnrepository.com/artifact/org.json/json -->
  195. <dependency>
  196. <groupId>org.json</groupId>
  197. <artifactId>json</artifactId>
  198. <version>20160810</version>
  199. </dependency>
  200. <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
  201. <dependency>
  202. <groupId>commons-codec</groupId>
  203. <artifactId>commons-codec</artifactId>
  204. <version>1.9</version>
  205. </dependency>
  206. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  207. <dependency>
  208. <groupId>com.alibaba</groupId>
  209. <artifactId>fastjson</artifactId>
  210. <version>1.2.47</version>
  211. </dependency>
  212. </dependencies>
  213. <build>
  214. <finalName>demo</finalName>
  215. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  216. <plugins>
  217. <plugin>
  218. <artifactId>maven-clean-plugin</artifactId>
  219. <version>3.1.0</version>
  220. </plugin>
  221. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  222. <plugin>
  223. <artifactId>maven-resources-plugin</artifactId>
  224. <version>3.0.2</version>
  225. </plugin>
  226. <plugin>
  227. <artifactId>maven-compiler-plugin</artifactId>
  228. <version>3.8.0</version>
  229. </plugin>
  230. <plugin>
  231. <artifactId>maven-surefire-plugin</artifactId>
  232. <version>2.22.1</version>
  233. </plugin>
  234. <plugin>
  235. <artifactId>maven-war-plugin</artifactId>
  236. <version>3.2.2</version>
  237. </plugin>
  238. <plugin>
  239. <artifactId>maven-install-plugin</artifactId>
  240. <version>2.5.2</version>
  241. </plugin>
  242. <plugin>
  243. <artifactId>maven-deploy-plugin</artifactId>
  244. <version>2.8.2</version>
  245. </plugin>
  246. </plugins>
  247. </pluginManagement>
  248. </build>
  249. </project>

在服务器端进行编码配置

现在我们开始编写UEController.java的代码,先写一个返回配置的方法:

  1. package com.blog.ueditor.controller;
  2. import com.baidu.ueditor.ActionEnter;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. /**
  10. * UEditor富文本编辑器,后端控制器
  11. * @author 秋枫艳梦
  12. * @date 2019-06-22
  13. **/
  14. @Controller
  15. @RequestMapping(value = "/ueditor")
  16. public class UEController {
  17. /**
  18. * 设置配置信息,供前端富文本编辑器检验是否可以使用IO流的操作
  19. * @param request 请求体
  20. * @param response 响应体
  21. * */
  22. @RequestMapping(value="/config")
  23. public void config(HttpServletRequest request, HttpServletResponse response) {
  24. response.setContentType("application/json");
  25. String rootPath = request.getSession().getServletContext().getRealPath("/");
  26. PrintWriter writer = null;
  27. try {
  28. String exec = new ActionEnter(request, rootPath).exec();
  29. writer = response.getWriter();
  30. writer.write(exec);
  31. writer.flush();
  32. System.out.println("UEditor富文本编辑器后端配置完毕");
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }finally {
  36. writer.close();
  37. }
  38. }
  39. }

在前端页面使用并配置UEditor编辑器

首先在home.html写下如下代码,唤起富文本编辑器(注意看引用的JS脚本):

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>UEditor编辑器教程</title>
  6. <!--不要忘了引jQuery-->
  7. <script src="../statics/js/jquery-3.2.1.min.js" type="text/javascript"></script>
  8. <script src="../utf8-jsp/ueditor.config.js"></script>
  9. <script src="../utf8-jsp/ueditor.all.min.js"></script>
  10. <script src="../utf8-jsp/lang/zh-cn/zh-cn.js"></script>
  11. </head>
  12. <body>
  13. <div>
  14. <textarea id="editor" style="width:100%;height:285px;"></textarea>
  15. </div>
  16. </body>
  17. <script>
  18. $(function(){
  19. //富文本编辑器
  20. UE.getEditor('editor');
  21. });
  22. </script>
  23. </html>

然后在utf8-jsp下的ueditor.config.js中做出如下配置:

 现在我们先不做别的,运行一下项目,页面上已经有富文本编辑器了,而且后台也打印了消息:

现在我们已经可以欢快的使用了,也支持小表情,标题、段落、字体加粗、样式等功能,可以说已经是一个小型的word文档编辑器了,如果你的项目只是这些需求,那么你已经整合完毕了,接下来的操作可以忽略~~~

实现上传图片的功能(上传附件、文件等同理)

大部分的需求,还是要上传图片或者附件的,甚至视频、其他文件,这里只以上传图片为例,第一是因为这个场景最常见,第二是因为其他的场景跟这个是一样的,只不过做不同的配置,让服务器端不同的处理方法去执行业务即可。

首先我们继续编写UEController.java,增加一个上传图片的处理方法:

  1. /**
  2. * 上传图片
  3. * @param file 上传的图片
  4. * @param request 请求体
  5. * @return Map类型的响应结果
  6. */
  7. @ResponseBody
  8. @RequestMapping(value = "/images", method = RequestMethod.POST)
  9. public Map<String, Object> images(@RequestParam(value = "imageFile", required = false) MultipartFile file, HttpServletRequest request) {
  10. Map<String, Object> params = new HashMap<String, Object>();
  11. if (file == null) {
  12. System.out.println("文件为空,方法结束");
  13. return null;
  14. }
  15. try {
  16. //为了简便,这里上传到桌面上的static文件夹
  17. String basePath = "C:\\Users\\Administrator\\Desktop\\static";
  18. String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
  19. String fileName = String.valueOf(System.currentTimeMillis()).concat("_").concat(String.valueOf(Math.random() * 1000) + 1000).concat(".").concat(ext);
  20. StringBuilder sb = new StringBuilder();
  21. //拼接保存路径
  22. sb.append(basePath).append("\\").append(fileName);
  23. String visitUrl = "http://localhost/ue/upload/" + fileName;
  24. File f = new File(sb.toString());
  25. if (!f.exists()) {
  26. f.getParentFile().mkdirs();
  27. }
  28. OutputStream out = new FileOutputStream(f);
  29. FileCopyUtils.copy(file.getInputStream(), out);
  30. params.put("state", "SUCCESS");
  31. params.put("url", visitUrl);
  32. params.put("size", file.getSize());
  33. params.put("original", fileName);
  34. params.put("type", file.getContentType());
  35. params.put("filename", file.getOriginalFilename());
  36. } catch (Exception e) {
  37. params.put("state", "ERROR");
  38. e.printStackTrace();
  39. }
  40. return params;
  41. }

然后,我们打开utf8-jsp下面的ueditor.config.js,做出如下配置:

注意:这里有坑,网上有说直接配置在config.json的,有说两个都要配置的,但博主亲测之后,发现只需要在ueditor.config.js配置即可,config.json可以说压根儿就没什么卵用(当然不能删除),官方文档说是config.json的优先级较低,但是我ueditor.config.js不配置,应该读取config.json啊?实际上配置config.json并不会有效。

 其中,imageActionName属性,是action的名字,具体是干嘛用的,待会再说;imageFieldName是上传的图片的名字,即参数名,对应我们后端处理方法的@RequestParam所指定的value属性。

我们继续修改home.html,做出如下修改:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>UEditor编辑器教程</title>
  6. <!--不要忘了引jQuery-->
  7. <script src="../statics/js/jquery-3.2.1.min.js" type="text/javascript"></script>
  8. <script src="../utf8-jsp/ueditor.config.js"></script>
  9. <script src="../utf8-jsp/ueditor.all.min.js"></script>
  10. <script src="../utf8-jsp/lang/zh-cn/zh-cn.js"></script>
  11. </head>
  12. <body>
  13. <div>
  14. <textarea id="editor" style="width:100%;height:285px;"></textarea>
  15. </div>
  16. </body>
  17. <script>
  18. $(function(){
  19. //富文本编辑器
  20. UE.getEditor('editor');
  21. UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
  22. UE.Editor.prototype.getActionUrl = function(action){
  23. if(action == 'action-image'){
  24. return 'http://localhost/ueditor/images';
  25. }else{
  26. return this._bkGetActionUrl.call(this,action);
  27. }
  28. }
  29. });
  30. </script>
  31. </html>

我们知道,UEditor可以在工具栏指定好多功能,上传图片、附件、文件等待,那它怎样区分这些操作呢?UE的研发团队以不同的action来区分,我们指定了上传图片的action为“action-image”,那么就要在用户使用这个功能时返回一个路径,这个路径就是处理这个action的,这里也就是我们后台上传图片的控制器的路径,这下子理解了吧?

所以,如果你想上传附件、文件、视频等等,在config.json配置对应的模块,然后在home.html做不同的action判断,分发给不同的后端服务器去处理即可。

运行项目,我们可以看到static文件夹下已经有上传的图片了,只是富文本编辑器没有回显图片:

 

 这是为什么呢?F12调试一下,发现富文本框明明有图片路径啊?

这是因为,我们的文件夹写在了tomcat服务器外部,这也是我们经常做的做法,否则服务器目录会愈发臃肿,所以我们需要做一下虚拟路径的映射,在tomcat的server.xml中配置:

 另外,在IDEA中调试的话,还需要勾选这一项:

OK,重新运行一下:

 已经达到我们想要的效果了,在项目中实际应用的话,再加一个提交事件,获取富文本框的内容,入库,不就完事了???

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/936934
推荐阅读
相关标签
  

闽ICP备14008679号