UEditor provides only the JSP version of the back-end entry code. However, the project source code is provided so that it can be modified according to business requirements.

The SpringBoot framework is used here, and the Thymeleaf template engine is equipped, so there is no need to add JSPS to be compatible with the UEditor; you can modify the source code to meet your needs. Below is a detailed tutorial.

1. Create the SpringBoot project and add the Web and Thymeleaf packages

Pom files are as follows:

[html]
view plain
copy
print
?

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

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "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. Example < / groupId > < artifactId > ueditor - test < / artifactId > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ueditor-test</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.5.2. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > <! --> <thymeleaf.version>3.0.3.RELEASE</thymeleaf.version> < thymeleaf - layout - the dialect version > 2.1.0 < / thymeleaf - layout - the dialect. Version > < / properties > < dependencies > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code


2. Download the source code from the official website and unzip it to the project. Note that config.json is copied to the resources root directory, as shown in the figure below:





3. Add UEditorController to the index page:

[java]
view plain
copy
print
?

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

package com.example; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by ldb on 2017/4/9. */ @Controller public class UEditorController { @RequestMapping("/") private String showPage(){ return "index"; }}Copy the code

4. Run the project. Access localhost:8080. If the following interface is displayed, the source code has been copied successfully





5. The image uploading function cannot be used. So let’s keep going. Modify poM to add Jar packages that UEditor depends on. Pom files are as follows:

[html]
view plain
copy
print
?

  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.   
  6.     <groupId>com.example</groupId >  
  7.     <artifactId>ueditor</artifactId >  
  8.     <version>0.0.1 – the SNAPSHOT</version >  
  9.     <packaging>jar</packaging>  
  10.   
  11.     <name>ueditor</name>  
  12.     <description>Demo project for Spring Boot</ description>  
  13.   
  14.     <parent>  
  15.         <groupId>org.springframework.boot</ groupId>  
  16.         <artifactId>spring-boot-starter-parent</ artifactId>  
  17.         <version>1.5.2. RELEASE</version >  
  18.         <relativePath/> <! — lookup parent from repository –>  
  19.     </parent>  
  20.   
  21.     <properties>  
  22.         <project.build.sourceEncoding>UTF-8</ project.build.sourceEncoding>  
  23.         <project.reporting.outputEncoding>UTF-8</ project.reporting.outputEncoding>  
  24.         <java.version>1.8</java.version >  
  25.         <thymeleaf.version>3.0.3. RELEASE</ thymeleaf.version>  
  26.         <thymeleaf-layout-dialect.version>2.1.0</ thymeleaf-layout-dialect.version>  
  27.     </properties>  
  28.   
  29.     <dependencies>  
  30.         <dependency>  
  31.             <groupId>org.springframework.boot</ groupId>  
  32.             <artifactId>spring-boot-starter-thymeleaf </artifactId>  
  33.         </dependency>  
  34.         <dependency>  
  35.             <groupId>org.springframework.boot</ groupId>  
  36.             <artifactId>spring-boot-starter-web</ artifactId>  
  37.         </dependency>  
  38.   
  39.         <dependency>  
  40.             <groupId>org.springframework.boot</ groupId>  
  41.             <artifactId>spring-boot-starter-test </artifactId>  
  42.             <scope>test</ scope>  
  43.         </dependency>  
  44.   
  45.         <! — Jar package that UEditor depends on –>  
  46.                 <dependency>  
  47.                     <groupId>org.json </groupId>  
  48.                     <artifactId>json </artifactId>  
  49.                 </dependency>  
  50.                 <dependency>  
  51.                     <groupId>commons-fileupload </groupId>  
  52.                     <artifactId>commons-fileupload </artifactId>  
  53.                     <version>1.3.2 </version>  
  54.                 </dependency>  
  55.                 <dependency>  
  56.                     <groupId>commons-codec </groupId>  
  57.                     <artifactId>commons-codec </artifactId>  
  58.                     <version>1.9 </version>  
  59.                 </dependency>  
  60.             </dependencies>  
  61.   
  62.             <build>  
  63.                 <plugins>  
  64.                     <plugin>  
  65.                         <groupId >org.springframework.boot</groupId>  
  66.                         <artifactId >spring-boot-maven-plugin</artifactId>  
  67.                     </plugin>  
  68.                 </plugins>  
  69.             </build>  
  70.   
  71.   
  72.         </project>  

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Example < / groupId > < artifactId > ueditor < / artifactId > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name> uEditor </name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.5.2. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > . < thymeleaf version > 3.0.3 RELEASE < / thymeleaf version > < thymeleaf - layout - the dialect version > 2.1.0 < / thymeleaf - layout - the dialect. Version > < / properties > < dependencies > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <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> <! Json </groupId> <artifactId>json</artifactId> </dependency> <dependency> < the groupId > Commons fileupload - < / groupId > < artifactId > Commons fileupload - < / artifactId > < version > 1.3.2 < / version > < / dependency > < the dependency > < groupId > Commons - codec < / groupId > < artifactId > Commons - codec < / artifactId > < version > 1.9 < / version > </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

6. Write the UEditorController class as described in controller.jsp. Map to config.

[java]
view plain
copy
print
?

  1. package com.example;  
  2.   
  3. import com.baidu.ueditor.ActionEnter;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import java.io.IOException;  
  10. import java.io.PrintWriter;  
  11.   
  12. / * * 
  13.  * Created by ldb on 2017/4/9. 
  14. * /  
  15. @Controller  
  16. public class UEditorController {  
  17.   
  18.   
  19.     @RequestMapping(“/”)  
  20.     private String showPage(){  
  21.         return “index”;  
  22.     }  
  23.   
  24.     @RequestMapping(value=“/config”)  
  25.     public void config(HttpServletRequest request, HttpServletResponse response) {  
  26.         response.setContentType(“application/json”);  
  27.         String rootPath = request.getSession().getServletContext().getRealPath(“/”);  
  28.         try {  
  29.             String exec = new ActionEnter(request, rootPath).exec();  
  30.             PrintWriter writer = response.getWriter();  
  31.             writer.write(exec);  
  32.             writer.flush();  
  33.             writer.close();  
  34.         } catch (IOException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.   
  38.     }  
  39. }  

package com.example; import com.baidu.ueditor.ActionEnter; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by ldb on 2017/4/9. */ @Controller public class UEditorController { @RequestMapping("/") private String showPage(){ return "index"; } @RequestMapping(value="/config") public void config(HttpServletRequest request, HttpServletResponse response) { response.setContentType("application/json"); String rootPath = request.getSession().getServletContext().getRealPath("/"); try { String exec = new ActionEnter(request, rootPath).exec(); PrintWriter writer = response.getWriter(); writer.write(exec); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); }}}Copy the code

7. Debug step by step that the config.json file cannot be loaded. Change the getConfigPath() method of the ConfigManage class. As follows:

[java]
view plain
copy
print
?

  1. package com.baidu.ueditor;  
  2.   
  3. import com.baidu.ueditor.define.ActionMap;  
  4. import org.json.JSONArray;  
  5. import org.json.JSONObject;  
  6.   
  7. import java.io.*;  
  8. import java.net.URISyntaxException;  
  9. import java.util.HashMap;  
  10. import java.util.Map;  
  11.   
  12. / * * 
  13. * Configuration manager 
  14.  * @author [email protected] 
  15.  * 
  16. * /  
  17. public final class ConfigManager {  
  18.   
  19.     private final String rootPath;  
  20.     private final String originalPath;  
  21.     private final String contextPath;  
  22.     private static final String configFileName =  “config.json”;  
  23.     private String parentPath = null;  
  24.     private JSONObject jsonConfig = null;  
  25.     // Doodle upload filename definition  
  26.     private final static String SCRAWL_FILE_NAME =  “scrawl”;  
  27.     // Remote image fetching filename definition  
  28.     private final static String REMOTE_FILE_NAME =  “remote”;  
  29.       
  30.     / * 
  31. * Build a configuration manager from a given path that requires the config.properties file to exist in the directory where the address path resides 
  32. * /  
  33.     private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException {  
  34.           
  35.         rootPath = rootPath.replace( “\\”, “/” );  
  36.           
  37.         this.rootPath = rootPath;  
  38.         this.contextPath = contextPath;  
  39.           
  40.         if ( contextPath.length() > 0 ) {  
  41.             this.originalPath = this.rootPath + uri.substring( contextPath.length() );  
  42.         } else {  
  43.             this.originalPath = this.rootPath + uri;  
  44.         }  
  45.           
  46.         this.initEnv();  
  47.           
  48.     }  
  49.       
  50.     / * * 
  51. * Configuration manager constructs factory 
  52. * @param rootPath server rootPath 
  53. * @param contextPath Project path of the server 
  54. * @param uri Specifies the uri of the current access 
  55. * @return Configuration manager instance or NULL 
  56. * /  
  57.     public static ConfigManager getInstance ( String rootPath, String contextPath, String uri ) {  
  58.           
  59.         try {  
  60.             return new ConfigManager(rootPath, contextPath, uri);  
  61.         } catch ( Exception e ) {  
  62.             return null;  
  63.         }  
  64.           
  65.     }  
  66.       
  67.     // Verify that the configuration file is correctly loaded  
  68.     public boolean valid () {  
  69.         return this.jsonConfig ! =null;  
  70.     }  
  71.       
  72.     public JSONObject getAllConfig () {  
  73.           
  74.         return this.jsonConfig;  
  75.           
  76.     }  
  77.       
  78.     public Map<String, Object> getConfig ( int type ) {  
  79.           
  80.         Map<String, Object> conf = new HashMap<String, Object>();  
  81.         String savePath = null;  
  82.           
  83.         switch ( type ) {  
  84.           
  85.             case ActionMap.UPLOAD_FILE:  
  86.                 conf.put( “isBase64”.“false” );  
  87.                 conf.put( “maxSize”.this.jsonConfig.getLong(  “fileMaxSize”));
  88.                 conf.put( “allowFiles”.this.getArray(  “fileAllowFiles”));
  89.                 conf.put( “fieldName”.this.jsonConfig.getString(  “fileFieldName”));
  90.                 savePath = this.jsonConfig.getString( “filePathFormat” );  
  91.                 break;  
  92.                   
  93.             case ActionMap.UPLOAD_IMAGE:  
  94.                 conf.put( “isBase64”.“false” );  
  95.                 conf.put( “maxSize”.this.jsonConfig.getLong(  “imageMaxSize”));
  96.                 conf.put( “allowFiles”.this.getArray(  “imageAllowFiles”));
  97.                 conf.put( “fieldName”.this.jsonConfig.getString(  “imageFieldName”));
  98.                 savePath = this.jsonConfig.getString( “imagePathFormat” );  
  99.                 break;  
  100.                   
  101.             case ActionMap.UPLOAD_VIDEO:  
  102.                 conf.put( “maxSize”.this.jsonConfig.getLong(  “videoMaxSize”));
  103.                 conf.put( “allowFiles”.this.getArray(  “videoAllowFiles”));
  104.                 conf.put( “fieldName”.this.jsonConfig.getString(  “videoFieldName”));
  105.                 savePath = this.jsonConfig.getString( “videoPathFormat” );  
  106.                 break;  
  107.                   
  108.             case ActionMap.UPLOAD_SCRAWL:  
  109.                 conf.put( “filename”, ConfigManager.SCRAWL_FILE_NAME );  
  110.                 conf.put( “maxSize”.this.jsonConfig.getLong(  “scrawlMaxSize”));
  111.                 conf.put( “fieldName”.this.jsonConfig.getString(  “scrawlFieldName”));
  112.                 conf.put( “isBase64”.“true” );  
  113.                 savePath = this.jsonConfig.getString( “scrawlPathFormat” );  
  114.                 break;  
  115.                   
  116.             case ActionMap.CATCH_IMAGE:  
  117.                 conf.put( “filename”, ConfigManager.REMOTE_FILE_NAME );  
  118.                 conf.put( “filter”.this.getArray(  “catcherLocalDomain”));
  119.                 conf.put( “maxSize”.this.jsonConfig.getLong(  “catcherMaxSize”));
  120.                 conf.put( “allowFiles”.this.getArray(  “catcherAllowFiles”));
  121.                 conf.put( “fieldName”.this.jsonConfig.getString(  “catcherFieldName” ) + “[]” );  
  122.                 savePath = this.jsonConfig.getString( “catcherPathFormat” );  
  123.                 break;  
  124.                   
  125.             case ActionMap.LIST_IMAGE:  
  126.                 conf.put( “allowFiles”.this.getArray(  “imageManagerAllowFiles”));
  127.                 conf.put( “dir”.this.jsonConfig.getString(  “imageManagerListPath”));
  128.                 conf.put( “count”.this.jsonConfig.getInt(  “imageManagerListSize”));
  129.                 break;  
  130.                   
  131.             case ActionMap.LIST_FILE:  
  132.                 conf.put( “allowFiles”.this.getArray(  “fileManagerAllowFiles”));
  133.                 conf.put( “dir”.this.jsonConfig.getString(  “fileManagerListPath”));
  134.                 conf.put( “count”.this.jsonConfig.getInt(  “fileManagerListSize”));
  135.                 break;  
  136.                   
  137.         }  
  138.           
  139.         conf.put( “savePath”, savePath );  
  140.         conf.put( “rootPath”.this.rootPath );  
  141.           
  142.         return conf;  
  143.           
  144.     }  
  145.       
  146.     private void initEnv () throws FileNotFoundException, IOException {  
  147.           
  148.         File file = new File( this.originalPath );  
  149.           
  150.         if ( !file.isAbsolute() ) {  
  151.             file = new File( file.getAbsolutePath() );  
  152.         }  
  153.           
  154.         this.parentPath = file.getParent();  
  155.           
  156.         String configContent = this.readFile( this.getConfigPath() );  
  157.           
  158.         try{  
  159.             JSONObject jsonConfig = new JSONObject( configContent );  
  160.             this.jsonConfig = jsonConfig;  
  161.         } catch ( Exception e ) {  
  162.             this.jsonConfig = null;  
  163.         }  
  164.           
  165.     }  
  166.   
  167.   
  168.     private String getConfigPath () {  
  169.         //return this.parentPath + File.separator + ConfigManager.configFileName;  
  170.         try {  
  171.             // Get the config.json path of classpath  
  172.             return this.getClass().getClassLoader().getResource( “config.json”).toURI().getPath();  
  173.         } catch (URISyntaxException e) {  
  174.             return null;  
  175.         }  
  176.     }  
  177.   
  178.     private String[] getArray ( String key ) {  
  179.           
  180.         JSONArray jsonArray = this.jsonConfig.getJSONArray( key );  
  181.         String[] result = new String[ jsonArray.length() ];  
  182.           
  183.         for ( int i = 0, len = jsonArray.length(); i < len; i++ ) {  
  184.             result[i] = jsonArray.getString( i );  
  185.         }  
  186.           
  187.         return result;  
  188.           
  189.     }  
  190.       
  191.     private String readFile ( String path ) throws IOException {  
  192.           
  193.         StringBuilder builder = new StringBuilder();  
  194.           
  195.         try {  
  196.               
  197.             InputStreamReader reader = new InputStreamReader( new FileInputStream( path ),  “UTF-8” );  
  198.             BufferedReader bfReader = new BufferedReader( reader );  
  199.               
  200.             String tmpContent = null;  
  201.               
  202.             while( ( tmpContent = bfReader.readLine() ) ! =null ) {  
  203.                 builder.append( tmpContent );  
  204.             }  
  205.               
  206.             bfReader.close();  
  207.               
  208.         } catch ( UnsupportedEncodingException e ) {  
  209.             / / ignore  
  210.         }  
  211.           
  212.         return this.filter( builder.toString() );  
  213.           
  214.     }  
  215.       
  216.     // Filter input strings, remove multi-line comments and replace backslashes  
  217.     private String filter ( String input ) {  
  218.           
  219.         return input.replaceAll( “/\\*[\\s\\S]*? \ \ * /”.“” );  
  220.           
  221.     }  
  222.       
  223. }  

package com.baidu.ueditor;

import com.baidu.ueditor.define.ActionMap;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 配置管理器
 * @author [email protected]
 *
 */
public final class ConfigManager {

	private final String rootPath;
	private final String originalPath;
	private final String contextPath;
	private static final String configFileName = "config.json";
	private String parentPath = null;
	private JSONObject jsonConfig = null;
	// 涂鸦上传filename定义
	private final static String SCRAWL_FILE_NAME = "scrawl";
	// 远程图片抓取filename定义
	private final static String REMOTE_FILE_NAME = "remote";
	
	/*
	 * 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件
	 */
	private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException {
		
		rootPath = rootPath.replace( "\\", "/" );
		
		this.rootPath = rootPath;
		this.contextPath = contextPath;
		
		if ( contextPath.length() > 0 ) {
			this.originalPath = this.rootPath + uri.substring( contextPath.length() );
		} else {
			this.originalPath = this.rootPath + uri;
		}
		
		this.initEnv();
		
	}
	
	/**
	 * 配置管理器构造工厂
	 * @param rootPath 服务器根路径
	 * @param contextPath 服务器所在项目路径
	 * @param uri 当前访问的uri
	 * @return 配置管理器实例或者null
	 */
	public static ConfigManager getInstance ( String rootPath, String contextPath, String uri ) {
		
		try {
			return new ConfigManager(rootPath, contextPath, uri);
		} catch ( Exception e ) {
			return null;
		}
		
	}
	
	// 验证配置文件加载是否正确
	public boolean valid () {
		return this.jsonConfig != null;
	}
	
	public JSONObject getAllConfig () {
		
		return this.jsonConfig;
		
	}
	
	public Map<String, Object> getConfig ( int type ) {
		
		Map<String, Object> conf = new HashMap<String, Object>();
		String savePath = null;
		
		switch ( type ) {
		
			case ActionMap.UPLOAD_FILE:
				conf.put( "isBase64", "false" );
				conf.put( "maxSize", this.jsonConfig.getLong( "fileMaxSize" ) );
				conf.put( "allowFiles", this.getArray( "fileAllowFiles" ) );
				conf.put( "fieldName", this.jsonConfig.getString( "fileFieldName" ) );
				savePath = this.jsonConfig.getString( "filePathFormat" );
				break;
				
			case ActionMap.UPLOAD_IMAGE:
				conf.put( "isBase64", "false" );
				conf.put( "maxSize", this.jsonConfig.getLong( "imageMaxSize" ) );
				conf.put( "allowFiles", this.getArray( "imageAllowFiles" ) );
				conf.put( "fieldName", this.jsonConfig.getString( "imageFieldName" ) );
				savePath = this.jsonConfig.getString( "imagePathFormat" );
				break;
				
			case ActionMap.UPLOAD_VIDEO:
				conf.put( "maxSize", this.jsonConfig.getLong( "videoMaxSize" ) );
				conf.put( "allowFiles", this.getArray( "videoAllowFiles" ) );
				conf.put( "fieldName", this.jsonConfig.getString( "videoFieldName" ) );
				savePath = this.jsonConfig.getString( "videoPathFormat" );
				break;
				
			case ActionMap.UPLOAD_SCRAWL:
				conf.put( "filename", ConfigManager.SCRAWL_FILE_NAME );
				conf.put( "maxSize", this.jsonConfig.getLong( "scrawlMaxSize" ) );
				conf.put( "fieldName", this.jsonConfig.getString( "scrawlFieldName" ) );
				conf.put( "isBase64", "true" );
				savePath = this.jsonConfig.getString( "scrawlPathFormat" );
				break;
				
			case ActionMap.CATCH_IMAGE:
				conf.put( "filename", ConfigManager.REMOTE_FILE_NAME );
				conf.put( "filter", this.getArray( "catcherLocalDomain" ) );
				conf.put( "maxSize", this.jsonConfig.getLong( "catcherMaxSize" ) );
				conf.put( "allowFiles", this.getArray( "catcherAllowFiles" ) );
				conf.put( "fieldName", this.jsonConfig.getString( "catcherFieldName" ) + "[]" );
				savePath = this.jsonConfig.getString( "catcherPathFormat" );
				break;
				
			case ActionMap.LIST_IMAGE:
				conf.put( "allowFiles", this.getArray( "imageManagerAllowFiles" ) );
				conf.put( "dir", this.jsonConfig.getString( "imageManagerListPath" ) );
				conf.put( "count", this.jsonConfig.getInt( "imageManagerListSize" ) );
				break;
				
			case ActionMap.LIST_FILE:
				conf.put( "allowFiles", this.getArray( "fileManagerAllowFiles" ) );
				conf.put( "dir", this.jsonConfig.getString( "fileManagerListPath" ) );
				conf.put( "count", this.jsonConfig.getInt( "fileManagerListSize" ) );
				break;
				
		}
		
		conf.put( "savePath", savePath );
		conf.put( "rootPath", this.rootPath );
		
		return conf;
		
	}
	
	private void initEnv () throws FileNotFoundException, IOException {
		
		File file = new File( this.originalPath );
		
		if ( !file.isAbsolute() ) {
			file = new File( file.getAbsolutePath() );
		}
		
		this.parentPath = file.getParent();
		
		String configContent = this.readFile( this.getConfigPath() );
		
		try{
			JSONObject jsonConfig = new JSONObject( configContent );
			this.jsonConfig = jsonConfig;
		} catch ( Exception e ) {
			this.jsonConfig = null;
		}
		
	}


	private String getConfigPath () {
		//return this.parentPath + File.separator + ConfigManager.configFileName;
		try {
			//获取classpath下的config.json路径
			return this.getClass().getClassLoader().getResource("config.json").toURI().getPath();
		} catch (URISyntaxException e) {
			return null;
		}
	}

	private String[] getArray ( String key ) {
		
		JSONArray jsonArray = this.jsonConfig.getJSONArray( key );
		String[] result = new String[ jsonArray.length() ];
		
		for ( int i = 0, len = jsonArray.length(); i < len; i++ ) {
			result[i] = jsonArray.getString( i );
		}
		
		return result;
		
	}
	
	private String readFile ( String path ) throws IOException {
		
		StringBuilder builder = new StringBuilder();
		
		try {
			
			InputStreamReader reader = new InputStreamReader( new FileInputStream( path ), "UTF-8" );
			BufferedReader bfReader = new BufferedReader( reader );
			
			String tmpContent = null;
			
			while ( ( tmpContent = bfReader.readLine() ) != null ) {
				builder.append( tmpContent );
			}
			
			bfReader.close();
			
		} catch ( UnsupportedEncodingException e ) {
			// 忽略
		}
		
		return this.filter( builder.toString() );
		
	}
	
	// 过滤输入字符串, 剔除多行注释以及替换掉反斜杠
	private String filter ( String input ) {
		
		return input.replaceAll( "/\\*[\\s\\S]*?\\*/", "" );
		
	}
	
}
Copy the code

this.getClass().getClassLoader().getResource(“config.json”).toURI().getPath(); 



You need to convert to URI and then getPath(), otherwise you will not be able to read the file if your project path has Spaces or Chinese characters

8. To run project path at http://localhost:8080/config? Action =config. If the following figure shows the config.json file can be read





9. Click upload and the picture will be displayed as follows



A message is displayed indicating that uploaded data is not found. The BinaryUploader class cannot get the byte stream



Google got the result because SpringMVC framework processed the request containing byte stream. This is the processed request, so the byte stream cannot be obtained. At this time, the parser multipartResolver of SpringMVC framework is used. Modify the source code as follows:

[java]
view plain
copy
print
?

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

package com.baidu.ueditor.upload; import com.baidu.ueditor.PathFormat; import com.baidu.ueditor.define.AppInfo; import com.baidu.ueditor.define.BaseState; import com.baidu.ueditor.define.FileType; import com.baidu.ueditor.define.State; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.List; import java.util.Map; 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; //InputStream is = fileStream.openStream(); InputStream is = multipartFile.getInputStream(); State storageState = 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); }}Copy the code


At this point to upload the image, it has been successfully uploaded.





10. But where did the picture go? Continue the debug discovery step by step and upload it to the path shown in the figure





The path shown in the figure is the Tomcat cache path. The file will be deleted as long as tomcat is restarted. We need to store it on disk. Modify the config.json file at this point.





The red arrow is the modification point. I need to store the files in E:/image/**. Here I added basePath to store static resources such as videos and music in E disk. Because basePath was added, the configuration needs to be modified. Go to ConfigManage with debug



Add the red arrow code to tuck basePath into the configuration file. Then go to the upload file class BinaryUploader and modify the following code:





To run the project, click Add Image. Open the image directory of drive E, as shown in the figure, and upload it to the corresponding path of drive E successfully



11. Open the browser and find that the page cannot load pictures. The diagram below:



Open the browser debugger. As shown in figure



Unable to get image. This is of course because we saved the image on drive E and Spring does not map the directory on drive E. Now we add a path map. Open the application.properties file and add the following code

[plain]
view plain
copy
print
?

  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:/pub lic/,file:${web.upload-path}

web.upload-path=E:/ spring.mvc.static-path-pattern=/** spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/pub lic/,file:${web.upload-path}Copy the code

At this point, re-run the project and click Upload image. The image will now display normally.





12. At this point, SpringBoot integration with the UEditor should be complete. Don’t worry, SpringBoot is a Jar package. Let’s use Maven to package and run it



java -jar 

Open the project address, click upload picture, find unexpectedly can not upload? !!!!!





What’s going on here? Why can not upload pictures after the Jar package. After constant debug and Google.. Found that the file cannot be read from the path of classLoader.getResource ().getPath() in the Jar package, use the Class getResourceAsStream() to read. Specific blog posts are as follows:

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

13. Let’s change the source code to getResourceAsStream to read config.json. Open the ConfigManager class and modify the initEnv method

[java]
view plain
copy
print
?

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

private void initEnv () throws FileNotFoundException, IOException {
		
		File file = new File( this.originalPath );
		
		if ( !file.isAbsolute() ) {
			file = new File( file.getAbsolutePath() );
		}
		
		this.parentPath = file.getParent();
		
		//String configContent = this.readFile( this.getConfigPath() );
		String configContent = this.filter(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("config.json")));

		try{
			JSONObject jsonConfig = new JSONObject( configContent );
			this.jsonConfig = jsonConfig;
		} catch ( Exception e ) {
			this.jsonConfig = null;
		}
		
	}Copy the code

14. Ok, pack up again and run the project







Success!!!!!!



The project source code: https://github.com/llldddbbb/ueditor-test

This is the end of the tutorial. Thank you for your attention