spring boot 若依系統整合Ueditor,部署時候上傳圖片錯誤解決
前言:國慶假期找了個ruoyi版本的cms玩玩,從git上看,介紹如下圖:
后臺部分截圖:
前臺blog截圖:
看上去還可以不錯,于是clone下來玩玩,結果發現,發布文章的時候,編輯器有問題,上傳不了圖片,還有其他幾個地方有問題,怎么解決呢?自己上手擼代碼,修改唄,于是,下載了ueditor的原始碼,加到專案中,進行修改,現在已經修改完成,并且也發布到的服務器上了,歡迎大家訪問測驗,文末會有凱哥修改后的git地址o~
正文:
在spring boot整合UEditor的時候,本地idea編輯器中沒問題,但是部署服務器上,上傳圖片提示:“后端配置項沒有正常加載,上傳插件不能正常使用!”解決辦法,
出現這種情況,可以很負責任的告訴你99%是因為,在加載的時候,沒有獲取到ueditor的config.json檔案,怎么處理了?
分析原因:
查看原來檔案存放位置:
在resources的static下,正常來說,是沒有問題的,但是spring boot打成jar包后的路徑和war包的路徑是不一樣的,檔案是在BOOT-INF下的,如下圖:
直接獲取,是不行的,找到原因后,我們就來想辦法解決掉,
解決步驟:
1:修改檔案存放位置,
如凱哥,直接就放在了resources下,檔案名稱為:ueditor-config.json(這個檔案名字,在后面需要用到),如下圖:
2:在yml檔案中,配置ueditor-config.json的檔案名:
uEditorConfig:
fileName: ueditor-config.json
如下圖:
3:撰寫一個controller(ps:JSP的凱哥沒有使用,修改成了controller.這樣符合習慣)
3.1:獲取json檔案名稱
需要注意:把第二步配置的檔案名稱,獲取到,如下圖:
3.2:撰寫獲取json的類(上傳的也寫在了里面),如下圖:
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 );
}
如下圖:
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;
}
}
如下圖:
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();
}
如下圖:
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"));
修改后,如下圖:
把ueditor.config.js檔案的serverUrl修改成第一步撰寫的controller對應的url.如下圖:
修改完成之后,重新打包之后,部署完成,發布訪問試試看,就可以了,
原始碼獲取:凱哥Java(kaigejava),回&|復:kaige-cms,即可獲取本系統原始碼了.blog體驗域名:www|jiahaoyou|net,將|換成.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/161748.html
標籤:Java
