SpringBoot+Thymeleaf 整合 Ueditor 上传图片

此处使用了SpringBoot框架,配备了Thymeleaf模板引擎,所以没有必要再添加jsp来兼容UEditor,可通过修改源码满足需要。下面是详细教程。

1.新建SpringBoot项目,添加web和thymeleaf包

pom文件如下:


  
  
  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.example </groupId>
  6. <artifactId>ueditor-test </artifactId>
  7. <version>0.0.1-SNAPSHOT </version>
  8. <packaging>jar </packaging>
  9. <name>ueditor-test </name>
  10. <description>Demo project for Spring Boot </description>
  11. <parent>
  12. <groupId>org.springframework.boot </groupId>
  13. <artifactId>spring-boot-starter-parent </artifactId>
  14. <version>1.5.2.RELEASE </version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
  20. <java.version>1.8 </java.version>
  21. <!--修改thymeleaf版本-->
  22. <thymeleaf.version>3.0.3.RELEASE </thymeleaf.version>
  23. <thymeleaf-layout-dialect.version>2.1.0 </thymeleaf-layout-dialect.version>
  24. </properties>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.boot </groupId>
  28. <artifactId>spring-boot-starter-thymeleaf </artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot </groupId>
  32. <artifactId>spring-boot-starter-web </artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot </groupId>
  36. <artifactId>spring-boot-starter-test </artifactId>
  37. <scope>test </scope>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot </groupId>
  44. <artifactId>spring-boot-maven-plugin </artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>

2.从官网下载源代码并解压至项目,注意config.json我拷到了resources根路径下,如图:



3.添加UEditorController,跳转到index页面:


  
  
  1. package com.example;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. /**
  5. * Created by ldb on 2017/4/9.
  6. */
  7. @Controller
  8. public class UEditorController {
  9. @RequestMapping( "/")
  10. private String showPage(){
  11. return "index";
  12. }
  13. }

4.运行项目。访问路径localhost:8080,跳转到如下界面即是源码已拷贝成功



5.此时发现上传图片功能不能用。下面接着看。修改pom,添加UEditor依赖的Jar包。pom文件如下: 


  
  
  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.example </groupId>
  6. <artifactId>ueditor </artifactId>
  7. <version>0.0.1-SNAPSHOT </version>
  8. <packaging>jar </packaging>
  9. <name>ueditor </name>
  10. <description>Demo project for Spring Boot </description>
  11. <parent>
  12. <groupId>org.springframework.boot </groupId>
  13. <artifactId>spring-boot-starter-parent </artifactId>
  14. <version>1.5.2.RELEASE </version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
  20. <java.version>1.8 </java.version>
  21. <thymeleaf.version>3.0.3.RELEASE </thymeleaf.version>
  22. <thymeleaf-layout-dialect.version>2.1.0 </thymeleaf-layout-dialect.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot </groupId>
  27. <artifactId>spring-boot-starter-thymeleaf </artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot </groupId>
  31. <artifactId>spring-boot-starter-web </artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot </groupId>
  35. <artifactId>spring-boot-starter-test </artifactId>
  36. <scope>test </scope>
  37. </dependency>
  38. <!--UEditor依赖的jar包 -->
  39. <dependency>
  40. <groupId>org.json </groupId>
  41. <artifactId>json </artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>commons-fileupload </groupId>
  45. <artifactId>commons-fileupload </artifactId>
  46. <version>1.3.2 </version>
  47. </dependency>
  48. <dependency>
  49. <groupId>commons-codec </groupId>
  50. <artifactId>commons-codec </artifactId>
  51. <version>1.9 </version>
  52. </dependency>
  53. </dependencies>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot </groupId>
  58. <artifactId>spring-boot-maven-plugin </artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>
6.照着源码里的controller.jsp.依样画葫芦,写入UEditorController类,映射路径为config。

  
  
  1. package com.example;
  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. * Created by ldb on 2017/4/9.
  11. */
  12. @Controller
  13. public class UEditorController {
  14. @RequestMapping( "/")
  15. private String showPage(){
  16. return "index";
  17. }
  18. @RequestMapping(value= "/config")
  19. public void config(HttpServletRequest request, HttpServletResponse response) {
  20. response.setContentType( "application/json");
  21. String rootPath = request.getSession().getServletContext().getRealPath( "/");
  22. try {
  23. String exec = new ActionEnter(request, rootPath).exec();
  24. PrintWriter writer = response.getWriter();
  25. writer.write(exec);
  26. writer.flush();
  27. writer.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
7.一步一步debug,发现无法加载config.json文件。此时修改ConfigManage类的getConfigPath()方法。如下:


  
  
  1. package com.baidu.ueditor;
  2. import com.baidu.ueditor.define.ActionMap;
  3. import org.json.JSONArray;
  4. import org.json.JSONObject;
  5. import java.io.*;
  6. import java.net.URISyntaxException;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. /**
  10. * 配置管理器
  11. * @author [email protected]
  12. *
  13. */
  14. public final class ConfigManager {
  15. private final String rootPath;
  16. private final String originalPath;
  17. private final String contextPath;
  18. private static final String configFileName = "config.json";
  19. private String parentPath = null;
  20. private JSONObject jsonConfig = null;
  21. // 涂鸦上传filename定义
  22. private final static String SCRAWL_FILE_NAME = "scrawl";
  23. // 远程图片抓取filename定义
  24. private final static String REMOTE_FILE_NAME = "remote";
  25. /*
  26. * 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件
  27. */
  28. private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException {
  29. rootPath = rootPath.replace( "\\", "/" );
  30. this.rootPath = rootPath;
  31. this.contextPath = contextPath;
  32. if ( contextPath.length() > 0 ) {
  33. this.originalPath = this.rootPath + uri.substring( contextPath.length() );
  34. } else {
  35. this.originalPath = this.rootPath + uri;
  36. }
  37. this.initEnv();
  38. }
  39. /**
  40. * 配置管理器构造工厂
  41. * @param rootPath 服务器根路径
  42. * @param contextPath 服务器所在项目路径
  43. * @param uri 当前访问的uri
  44. * @return 配置管理器实例或者null
  45. */
  46. public static ConfigManager getInstance ( String rootPath, String contextPath, String uri ) {
  47. try {
  48. return new ConfigManager(rootPath, contextPath, uri);
  49. } catch ( Exception e ) {
  50. return null;
  51. }
  52. }
  53. // 验证配置文件加载是否正确
  54. public boolean valid () {
  55. return this.jsonConfig != null;
  56. }
  57. public JSONObject getAllConfig () {
  58. return this.jsonConfig;
  59. }
  60. public Map<String, Object> getConfig ( int type ) {
  61. Map<String, Object> conf = new HashMap<String, Object>();
  62. String savePath = null;
  63. switch ( type ) {
  64. case ActionMap.UPLOAD_FILE:
  65. conf.put( "isBase64", "false" );
  66. conf.put( "maxSize", this.jsonConfig.getLong( "fileMaxSize" ) );
  67. conf.put( "allowFiles", this.getArray( "fileAllowFiles" ) );
  68. conf.put( "fieldName", this.jsonConfig.getString( "fileFieldName" ) );
  69. savePath = this.jsonConfig.getString( "filePathFormat" );
  70. break;
  71. case ActionMap.UPLOAD_IMAGE:
  72. conf.put( "isBase64", "false" );
  73. conf.put( "maxSize", this.jsonConfig.getLong( "imageMaxSize" ) );
  74. conf.put( "allowFiles", this.getArray( "imageAllowFiles" ) );
  75. conf.put( "fieldName", this.jsonConfig.getString( "imageFieldName" ) );
  76. savePath = this.jsonConfig.getString( "imagePathFormat" );
  77. break;
  78. case ActionMap.UPLOAD_VIDEO:
  79. conf.put( "maxSize", this.jsonConfig.getLong( "videoMaxSize" ) );
  80. conf.put( "allowFiles", this.getArray( "videoAllowFiles" ) );
  81. conf.put( "fieldName", this.jsonConfig.getString( "videoFieldName" ) );
  82. savePath = this.jsonConfig.getString( "videoPathFormat" );
  83. break;
  84. case ActionMap.UPLOAD_SCRAWL:
  85. conf.put( "filename", ConfigManager.SCRAWL_FILE_NAME );
  86. conf.put( "maxSize", this.jsonConfig.getLong( "scrawlMaxSize" ) );
  87. conf.put( "fieldName", this.jsonConfig.getString( "scrawlFieldName" ) );
  88. conf.put( "isBase64", "true" );
  89. savePath = this.jsonConfig.getString( "scrawlPathFormat" );
  90. break;
  91. case ActionMap.CATCH_IMAGE:
  92. conf.put( "filename", ConfigManager.REMOTE_FILE_NAME );
  93. conf.put( "filter", this.getArray( "catcherLocalDomain" ) );
  94. conf.put( "maxSize", this.jsonConfig.getLong( "catcherMaxSize" ) );
  95. conf.put( "allowFiles", this.getArray( "catcherAllowFiles" ) );
  96. conf.put( "fieldName", this.jsonConfig.getString( "catcherFieldName" ) + "[]" );
  97. savePath = this.jsonConfig.getString( "catcherPathFormat" );
  98. break;
  99. case ActionMap.LIST_IMAGE:
  100. conf.put( "allowFiles", this.getArray( "imageManagerAllowFiles" ) );
  101. conf.put( "dir", this.jsonConfig.getString( "imageManagerListPath" ) );
  102. conf.put( "count", this.jsonConfig.getInt( "imageManagerListSize" ) );
  103. break;
  104. case ActionMap.LIST_FILE:
  105. conf.put( "allowFiles", this.getArray( "fileManagerAllowFiles" ) );
  106. conf.put( "dir", this.jsonConfig.getString( "fileManagerListPath" ) );
  107. conf.put( "count", this.jsonConfig.getInt( "fileManagerListSize" ) );
  108. break;
  109. }
  110. conf.put( "savePath", savePath );
  111. conf.put( "rootPath", this.rootPath );
  112. return conf;
  113. }
  114. private void initEnv () throws FileNotFoundException, IOException {
  115. File file = new File( this.originalPath );
  116. if ( !file.isAbsolute() ) {
  117. file = new File( file.getAbsolutePath() );
  118. }
  119. this.parentPath = file.getParent();
  120. String configContent = this.readFile( this.getConfigPath() );
  121. try{
  122. JSONObject jsonConfig = new JSONObject( configContent );
  123. this.jsonConfig = jsonConfig;
  124. } catch ( Exception e ) {
  125. this.jsonConfig = null;
  126. }
  127. }
  128. private String getConfigPath () {
  129. //return this.parentPath + File.separator + ConfigManager.configFileName;
  130. try {
  131. //获取classpath下的config.json路径
  132. return this.getClass().getClassLoader().getResource( "config.json").toURI().getPath();
  133. } catch (URISyntaxException e) {
  134. return null;
  135. }
  136. }
  137. private String[] getArray ( String key ) {
  138. JSONArray jsonArray = this.jsonConfig.getJSONArray( key );
  139. String[] result = new String[ jsonArray.length() ];
  140. for ( int i = 0, len = jsonArray.length(); i < len; i++ ) {
  141. result[i] = jsonArray.getString( i );
  142. }
  143. return result;
  144. }
  145. private String readFile ( String path ) throws IOException {
  146. StringBuilder builder = new StringBuilder();
  147. try {
  148. InputStreamReader reader = new InputStreamReader( new FileInputStream( path ), "UTF-8" );
  149. BufferedReader bfReader = new BufferedReader( reader );
  150. String tmpContent = null;
  151. while ( ( tmpContent = bfReader.readLine() ) != null ) {
  152. builder.append( tmpContent );
  153. }
  154. bfReader.close();
  155. } catch ( UnsupportedEncodingException e ) {
  156. // 忽略
  157. }
  158. return this.filter( builder.toString() );
  159. }
  160. // 过滤输入字符串, 剔除多行注释以及替换掉反斜杠
  161. private String filter ( String input ) {
  162. return input.replaceAll( "/\\*[\\s\\S]*?\\*/", "" );
  163. }
  164. }
this.getClass().getClassLoader().getResource(“config.json”).toURI().getPath(); 
此处需要先转为URI再getPath(),否则如果你的项目路径带空格或者带中文则无法读取到文件

8.运行项目路径http://localhost:8080/config?action=config,如下图显示则表示可读取到config.json文件



9.此时点击上传图片显示 如下


提示未找到上传数据。继续一步步debug,发现在BinaryUploader类竟然无法获取到字节流


google得到原因是因为SpringMVC框架对含字节流的request进行了处理,此处传的是处理过的request,故获取不到字节流。此时采用SpringMVC框架的解析器multipartResolver。修改源码如下:


  
  
  1. package com.baidu.ueditor.upload;
  2. import com.baidu.ueditor.PathFormat;
  3. import com.baidu.ueditor.define.AppInfo;
  4. import com.baidu.ueditor.define.BaseState;
  5. import com.baidu.ueditor.define.FileType;
  6. import com.baidu.ueditor.define.State;
  7. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import org.springframework.web.multipart.MultipartHttpServletRequest;
  10. import javax.servlet.http.HttpServletRequest;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.Arrays;
  14. import java.util.List;
  15. import java.util.Map;
  16. public class BinaryUploader {
  17. public static final State save(HttpServletRequest request,
  18. Map<String, Object> conf) {
  19. // FileItemStream fileStream = null;
  20. // boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;
  21. if (!ServletFileUpload.isMultipartContent(request)) {
  22. return new BaseState( false, AppInfo.NOT_MULTIPART_CONTENT);
  23. }
  24. // ServletFileUpload upload = new ServletFileUpload(
  25. // new DiskFileItemFactory());
  26. //
  27. // if ( isAjaxUpload ) {
  28. // upload.setHeaderEncoding( "UTF-8" );
  29. // }
  30. try {
  31. // FileItemIterator iterator = upload.getItemIterator(request);
  32. //
  33. // while (iterator.hasNext()) {
  34. // fileStream = iterator.next();
  35. //
  36. // if (!fileStream.isFormField())
  37. // break;
  38. // fileStream = null;
  39. // }
  40. //
  41. // if (fileStream == null) {
  42. // return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
  43. // }
  44. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  45. MultipartFile multipartFile = multipartRequest.getFile(conf.get( "fieldName").toString());
  46. if(multipartFile== null){
  47. return new BaseState( false, AppInfo.NOTFOUND_UPLOAD_DATA);
  48. }
  49. String savePath = (String) conf.get( "savePath");
  50. //String originFileName = fileStream.getName();
  51. String originFileName = multipartFile.getOriginalFilename();
  52. String suffix = FileType.getSuffixByFilename(originFileName);
  53. originFileName = originFileName.substring( 0,
  54. originFileName.length() - suffix.length());
  55. savePath = savePath + suffix;
  56. long maxSize = ((Long) conf.get( "maxSize")).longValue();
  57. if (!validType(suffix, (String[]) conf.get( "allowFiles"))) {
  58. return new BaseState( false, AppInfo.NOT_ALLOW_FILE_TYPE);
  59. }
  60. savePath = PathFormat.parse(savePath, originFileName);
  61. String physicalPath = (String) conf.get( "rootPath") + savePath;
  62. //InputStream is = fileStream.openStream();
  63. InputStream is = multipartFile.getInputStream();
  64. State storageState = StorageManager.saveFileByInputStream(is,
  65. physicalPath, maxSize);
  66. is.close();
  67. if (storageState.isSuccess()) {
  68. storageState.putInfo( "url", PathFormat.format(savePath));
  69. storageState.putInfo( "type", suffix);
  70. storageState.putInfo( "original", originFileName + suffix);
  71. }
  72. return storageState;
  73. // } catch (FileUploadException e) {
  74. // return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
  75. } catch (IOException e) {
  76. }
  77. return new BaseState( false, AppInfo.IO_ERROR);
  78. }
  79. private static boolean validType(String type, String[] allowTypes) {
  80. List<String> list = Arrays.asList(allowTypes);
  81. return list.contains(type);
  82. }
  83. }

此时进行上传图片,已经能够成功上传了。



10.可是图片究竟上传到哪里了呢?继续一步步debug发现,上传到如图路径



如图路径为tomcat缓存路径,只要重启下tomcat该文件就会被删除。我们需要将其存储到磁盘中。此时修改config.json文件。



红色箭头为修改处。我需要将文件存储到E:/image/**下,此处我多添加了basePath,是想把视频、音乐等静态资源都存储到E盘。由于添加了basePath,需要修改配置。通过debug来到ConfigManage


添加红色箭头代码,将basePath塞进配置文件里。之后继续来到上传文件类BinaryUploader,修改如下代码:



运行项目,点击添加图片。打开E盘的image目录,如图,成功上传到E盘对应路径


11.打开浏览器,发现页面无法加载图片。如下图:


打开浏览器调试器。如图


无法获取到图片。这是当然的,因为我们把图片存在E盘了,而spring并没有对E盘目录进行映射。此时我们加入路径映射。打开application.properties文件,添加如下代码


  
  
  1. web.upload-path=E:/
  2. spring.mvc. static-path-pattern= /**
  3. spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}

此时重新运行项目,点击上传图片,图片已经能够正常显示了。



12.至此,SpringBoot整合UEditor应该完了吧。别急,SpringBoot主张打包成Jar包运行,我们用Maven来打包运行试


java -jar 

打开项目地址,点击上传图片,发现竟然上传不了了??!!



这是怎么回事呢?为什么打成Jar包后就无法上传图片了呢。经过不断的debug和google。。发现了在Jar包里无法以ClassLoader.getResource().getPath()获得的路径读取文件,得用Class类的getResourceAsStream()来读取。具体博文如下:

http://hxraid.iteye.com/blog/483115?page=3#comments

13.那么我们就来修改源码,改成getResourceAsStream读取config.json文件吧。打开ConfigManager类,修改initEnv方法


  
  
  1. private void initEnv () throws FileNotFoundException, IOException {
  2. File file = new File( this.originalPath );
  3. if ( !file.isAbsolute() ) {
  4. file = new File( file.getAbsolutePath() );
  5. }
  6. this.parentPath = file.getParent();
  7. //String configContent = this.readFile( this.getConfigPath() );
  8. String configContent = this.filter(IOUtils.toString( this.getClass().getClassLoader().getResourceAsStream( "config.json")));
  9. try{
  10. JSONObject jsonConfig = new JSONObject( configContent );
  11. this.jsonConfig = jsonConfig;
  12. } catch ( Exception e ) {
  13. this.jsonConfig = null;
  14. }
  15. }

14. ok了,再次打包,运行项目




成功了!!!


猜你喜欢

转载自blog.csdn.net/GJX_BLOG/article/details/82080673