項目中使用UEditor發現設置圖片自定義保存路徑會出現《請求后台配置項http錯誤,上傳功能將不能正常使用!錯誤》
/* 上傳圖片配置項 */ "imageActionName": "uploadimage", /* 執行上傳圖片的action名稱 */ "imageFieldName": "inputForm", /* 提交的圖片表單名稱 */ "imageMaxSize": 1024000, /* 上傳大小限制,單位B */ "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上傳圖片格式顯示 */ "imageCompressEnable": true, /* 是否壓縮圖片,默認是true */ "imageCompressBorder": 1600, /* 圖片壓縮最長邊限制 */ "imageInsertAlign": "none", /* 插入的圖片浮動方式 */ "imageUrlPrefix": "/cms/static/userfiles/", /* 圖片訪問路徑前綴 */ "imagePathFormat": "/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */
原因在於UEditor提供的 ueditor-1.1.2.jar 之中的 ConfigManager 類獲取不到 config.json 文件
上傳圖片會獲取controller.jsp地址
//ueditor.config.js
var URL = window.UEDITOR_HOME_URL || getUEBasePath(); /** * 配置項主體。注意,此處所有涉及到路徑的配置別遺漏URL變量。 */ window.UEDITOR_CONFIG = { //為編輯器實例添加一個路徑,這個不能被注釋 UEDITOR_HOME_URL: URL // 服務器統一請求接口路徑 , serverUrl: URL + "jsp/controller.jsp"
執行controller.jsp,rootPath 是要保存圖片的路徑
//controller.jsp <% request.setCharacterEncoding( "utf-8" ); response.setHeader("Content-Type" , "text/html"); String rootPath = application.getRealPath( "/" ); out.write( new ActionEnter( request, rootPath ).exec() ); %>
修改后
<% request.setCharacterEncoding("utf-8"); response.setHeader("Content-Type", "text/html"); // String rootPath = application.getRealPath( "/" ); String rootPath = Global.getConfig("userfiles.basedir");//圖片保存目錄,會與imagePathFormat拼接起來,此為主要保存路徑,imagePathFormat可以只設置圖片名稱 String jsonPath = Global.getConfig("userfiles.jsonPash"); //config.json 目錄 out.write(new ActionEnter(request, rootPath, jsonPath).exec()); %>
ActionEnter執行了 ConfigManager.getInstance 傳入了 request.getRequestURI()
request.getRequestURI()獲取的是 controller.jsp文件的相對路徑
//ActionEnter類
public ActionEnter(HttpServletRequest request, String rootPath) { 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()); }
修改后
public ActionEnter(HttpServletRequest request, String rootPath, String jsonPath) { this.request = request; this.rootPath = rootPath; this.actionType = request.getParameter("action"); this.contextPath = request.getContextPath(); this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, jsonPath); }
此時 ConfigManager 方法用你的圖片保存路徑加上 controller.jsp 的相對路徑去找 config.json,就出現找不到的情況,可以修改jar包,把 request.getRequestURI()更改為變量,以傳參方式把地址傳進來,再把if去掉,直接使用傳 uri 給 File
//ConfigManager類
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; private static final String SCRAWL_FILE_NAME = "scrawl"; private static final String REMOTE_FILE_NAME = "remote"; 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(); } public static ConfigManager getInstance(String rootPath, String contextPath, String uri) { try { return new ConfigManager(rootPath, contextPath, uri); } catch (Exception var4) { return null; } } //獲取 config.json 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 var4) { this.jsonConfig = null; } } //獲取 config.json 路徑 private String getConfigPath() { return this.parentPath + File.separator + "config.json"; } }
修改后
private ConfigManager(String rootPath, String contextPath, String jsonPath) throws FileNotFoundException, IOException { rootPath = rootPath.replace("\\", "/"); this.rootPath = rootPath; this.contextPath = contextPath; this.originalPath = jsonPath; this.initEnv(); }
圖片上傳成功