If spring boot integrates Ueditor according to the system, the uploading picture error is solved during deployment

If spring boot integrates Ueditor according to the system, the uploading picture error is solved during deployment

Preface: I found a ruoyi version of cms to play with during the National Day holiday. From git, the introduction is as follows:

Screenshot of the background part:

0kcK7bIeoSm

edit

0kcK7gX63YO

edit

0kcK7iZVNVg

edit

0kcK7iDVRui

edit

Screenshot of the front desk blog:

0kcK7lJbBaq

edit

0kcK7nK7rma

edit

It looks pretty good, so clone came down to play, and found out that when publishing the article, there was a problem with the editor, the picture could not be uploaded, and there were several other problems. How to solve it? Get started by yourself and modify the code. So, I downloaded the source code of ueditor, added it to the project, and modified it. Now that the modification has been completed and it has been published to the server, you are welcome to visit the test. At the end of the article, there will be Kai Ge's modified git address o~

text:

When spring boot integrates UEditor, there is no problem in the local idea editor, but on the deployment server, the upload image prompts: "The back-end configuration item is not loaded normally, and the upload plug-in cannot be used normally! " Solution.

When this happens, I can tell you 99% responsibly because the ueditor config.json file was not obtained when loading. How to deal with it?

Analyze the reasons:

View the original file storage location:

Normally, there is no problem under the static of resources. But the path after spring boot is marked as jar package is different from the path of war package. The file is under BOOT-INF. As shown below:

0kcK7nEw6CG

edit

Obtaining directly is not acceptable. After finding the cause, we come to think of a way to solve it.

Solution steps:

1: Modify the file storage location.

For example, Brother Kai, put it directly under resources, and the file name is: ueditor-config.json (this file name will be used later). As shown below:

0kcK7oU2xGa

edit

2: In the yml file, configure the file name of ueditor-config.json:

uEditorConfig:
  fileName: ueditor-config.json

As shown below:

0kcK7nnT2tU

edit

3: Write a controller (ps: Brother Kai of JSP did not use it, modified it to controller. This conforms to the habit)

3.1: Get the json file name

需要注意:把第二步配置的文件名称,获取到。如下图:

0kcK7o3fv84

编辑

3.2:编写获取json的类(上传的也写在了里面)。如下图:

0kcK7oa4QGO

编辑

4:修改Ueditor的源码

4.1:ActionEnter类的构造方法重写。

/**
 * 获取config.json的
 * @param request
 * @param rootPath
 * @param configFileName
 */
public ActionEnter (HttpServletRequest request, String rootPath,String configFileName ) {
   this.request = request;
   this.rootPath = rootPath;
   this.actionType = request.getParameter( "action" );
   this.contextPath = request.getContextPath();
   this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI(),configFileName );
}

如下图:

0kcK7q1SqNk

编辑

4.2:重写ConfigManager.getInstance方法

/**
 * 配置管理器构造工厂--修改后
 * @param rootPath 服务器根路径
 * @param contextPath 服务器所在项目路径
 * @param uri 当前访问的uri
 * @param configFileName config.json的文件名称
 * @return 配置管理器实例或者null
 */
public static ConfigManager getInstance ( String rootPath, String contextPath, String uri,String configFileName  ) {

   try {
      return new ConfigManager(rootPath, contextPath, uri,configFileName);
   } catch ( Exception e ) {
      return null;
   }

}

如下图:

0kcK7pOGblg

编辑

4.3:重写ConfigManager构造器

/*
 * 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件--kaigejava修改
 */
private ConfigManager ( String rootPath, String contextPath, String uri,String configFileName) throws FileNotFoundException, IOException {

   rootPath = rootPath.replace( "\\", "/" );

   this.rootPath = rootPath;
   this.contextPath = contextPath;
   this.configFileName = configFileName;

   if ( contextPath.length() > 0 ) {
      this.originalPath = this.rootPath + uri.substring( contextPath.length() );
   } else {
      this.originalPath = this.rootPath + uri;
   }

   this.initEnv();
}

如下图:

0kcK7pmZDMm

编辑

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(configFileName),"UTF-8"));
   try{
      JSONObject jsonConfig = JSONObject.parseObject(configContent);
      this.jsonConfig = jsonConfig;
   } catch ( Exception e ) {
      this.jsonConfig = null;
   }
   
}

其中核心的:

 String configContent = this.filter(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(configFileName),"UTF-8"));

After modification, as shown below:

0kcK7qFnoP2

edit

Modify the serverUrl of the ueditor.config.js file to the url corresponding to the controller written in the first step. As shown below:

0kcK7qYToa8

edit

After the modification is completed, after repackaging, the deployment is complete, and the release visit is a try. That's it.

Source code acquisition: Kaige Java (kaigejava) , reply &| reply: kaige-cms. You can get the source code of this system. Blog experience domain name: www|jiahaoyou|net. Replace | with.


Guess you like

Origin blog.51cto.com/kaigejava/2540285