SSM整合項目中使用百度Ueditor遇到的問題。

問題描述:沒法上傳圖片,提示配置項加載有問題html

大體情形:直接下載的ue編輯器,放在了/resources/   目錄下,也就是靜態資源路徑,而後更改web.xml,將tomcat默認攔截器配置放到全部servlet的最上面前端

1     <!-- 不攔截靜態文件 -->  
2     <servlet-mapping>  
3         <servlet-name>default</servlet-name>  
4         <url-pattern>/resources/*</url-pattern>  
5     </servlet-mapping>  

可即便是這樣,打開demo界面,點圖片上傳,提示後端配置未正常加載,手動輸入百度ue的controller.jsp地址時卻輸出的是controller.jsp的源碼,不放棄,繼續:java

查看官方文檔得知默認會訪問controller.jsp是由於以下配置項(ps:圖是改事後的,非默認配置),既然這樣,那我們就本身定義個controller,把controller.jsp的代碼copy到本身的controller裏面,而後更改圖中配置項來處理ue的請求:git

說作就作:github

 1 package **********************************;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 
10 import com.baidu.ueditor.ActionEnter;
11 
12 @Controller
13 @RequestMapping("/ueditor")
14 public class UeditorController {
15 
16     @RequestMapping("/upload")
17     public void exec(@RequestParam(value="action",required=false)String action,
18             @RequestParam(value="callback",required=false)String callback,
19             HttpServletRequest req, HttpServletResponse res) {
20         try {
21             req.setCharacterEncoding("utf-8");
22             res.setHeader("Content-Type", "text/html");
23             String rootPath =req.getSession().getServletContext().getRealPath("/");
24             res.getWriter().write(new ActionEnter(req, rootPath,"").exec());
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28     }
29 }

這下應該能夠了吧,no,no,no,仍是提示後端服務爲正常加載,debug模式走起,原來百度ue有個小問題,概括起來以下:web

首先確定要加載全部配置,java語言的配置是在config.json中配置的,而後引伸出另外一個問題,如何獲取config.json的真實路徑,spring

原來百度的大神們是這樣作的:假設大家能夠訪問到ue的初始頁面,也就是能夠看到編輯框,那麼ue在編輯框初始化的時候就發個詢問後端配置是否正常的get請求,就是ueditor.config.js中配置的serverUrl,而後加個參數 ?action=config,代碼裏最後走這行代碼:json

1 res.getWriter().write(new ActionEnter(req, rootPath,"").exec());

rootpath是項目的真實物理路徑,然後用request.getRequestURI獲取訪問路徑,二者加到一塊兒,就獲取到了config.json文件所在路徑的文件夾(假設仍是訪問的controller.jsp,那麼是沒問題的.ps:一改問題就多。。。),這樣就能夠加載config.json文件中的全部配置了。然而spring mvc可隨意定義url路徑,因此,這樣作就有問題了,因此就出了上面那個配置項有問題沒法上傳的提示了。那怎麼解決呢?在我走了一段彎路以後怒了,大爺的不就是要加載config.json嗎?何須繞那麼大的圈子,我直接給你不就完了?因而乎,下載ue的jar包源碼,更改ActionEnter的構造方法,將路徑傳過去,最後的ActionEnter以下:後端

  1 package com.baidu.ueditor;
  2 
  3 import java.util.Map;
  4 
  5 import javax.servlet.http.HttpServletRequest;
  6 
  7 import com.baidu.ueditor.define.ActionMap;
  8 import com.baidu.ueditor.define.AppInfo;
  9 import com.baidu.ueditor.define.BaseState;
 10 import com.baidu.ueditor.define.State;
 11 import com.baidu.ueditor.hunter.FileManager;
 12 import com.baidu.ueditor.hunter.ImageHunter;
 13 import com.baidu.ueditor.upload.Uploader;
 14 
 15 public class ActionEnter {
 16     
 17     private HttpServletRequest request = null;
 18     
 19     private String rootPath = null;
 20     private String contextPath = null;
 21     
 22     private String actionType = null;
 23     
 24     private ConfigManager configManager = null;
 25 
 26     public ActionEnter ( HttpServletRequest request, String rootPath,String jsonFilePath ) {
 27         
 28         this.request = request;
 29         this.rootPath = rootPath;
 30         this.actionType = request.getParameter( "action" );
 31         this.contextPath = request.getContextPath();
 32         this.configManager = ConfigManager.getInstance( this.rootPath,jsonFilePath, this.contextPath, request.getRequestURI().replace(request.getContextPath(),"") );
 33         
 34     }
 35     
 36     public String exec () {
 37         
 38         String callbackName = this.request.getParameter("callback");
 39         
 40         if ( callbackName != null ) {
 41 
 42             if ( !validCallbackName( callbackName ) ) {
 43                 return new BaseState( false, AppInfo.ILLEGAL ).toJSONString();
 44             }
 45             
 46             return callbackName+"("+this.invoke()+");";
 47             
 48         } else {
 49             return this.invoke();
 50         }
 51 
 52     }
 53     
 54     public String invoke() {
 55         
 56         if ( actionType == null || !ActionMap.mapping.containsKey( actionType ) ) {
 57             return new BaseState( false, AppInfo.INVALID_ACTION ).toJSONString();
 58         }
 59         
 60         if ( this.configManager == null || !this.configManager.valid() ) {
 61             return new BaseState( false, AppInfo.CONFIG_ERROR ).toJSONString();
 62         }
 63         
 64         State state = null;
 65         
 66         int actionCode = ActionMap.getType( this.actionType );
 67         
 68         Map<String, Object> conf = null;
 69         
 70         switch ( actionCode ) {
 71         
 72             case ActionMap.CONFIG:
 73                 return this.configManager.getAllConfig().toString();
 74                 
 75             case ActionMap.UPLOAD_IMAGE:
 76             case ActionMap.UPLOAD_SCRAWL:
 77             case ActionMap.UPLOAD_VIDEO:
 78             case ActionMap.UPLOAD_FILE:
 79                 conf = this.configManager.getConfig( actionCode );
 80                 state = new Uploader( request, conf ).doExec();
 81                 break;
 82                 
 83             case ActionMap.CATCH_IMAGE:
 84                 conf = configManager.getConfig( actionCode );
 85                 String[] list = this.request.getParameterValues( (String)conf.get( "fieldName" ) );
 86                 state = new ImageHunter( conf ).capture( list );
 87                 break;
 88                 
 89             case ActionMap.LIST_IMAGE:
 90             case ActionMap.LIST_FILE:
 91                 conf = configManager.getConfig( actionCode );
 92                 int start = this.getStartIndex();
 93                 state = new FileManager( conf ).listFile( start );
 94                 break;
 95                 
 96         }
 97         
 98         return state.toJSONString();
 99         
100     }
101     
102     public int getStartIndex () {
103         
104         String start = this.request.getParameter( "start" );
105         
106         try {
107             return Integer.parseInt( start );
108         } catch ( Exception e ) {
109             return 0;
110         }
111         
112     }
113     
114     /**
115      * callback參數驗證
116      */
117     public boolean validCallbackName ( String name ) {
118         
119         if ( name.matches( "^[a-zA-Z_]+[\\w0-9_]*$" ) ) {
120             return true;
121         }
122         
123         return false;
124         
125     }
126     
127 }
View Code

終於成功了,tomcat

覺得終於能夠結束了,然而並非,點擊上傳圖片老提示沒有發現上傳的數據,再次debug源碼,spring mvc對request進行了包裝,致使沒法獲取上傳數據,無語了,那就直接換servlet處理,上代碼:

 1 package *************************;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import com.baidu.ueditor.ActionEnter;
11 
12 
13 public class UeditorController extends HttpServlet{
14 
15     /**
16      * 
17      */
18     private static final long serialVersionUID = 10001L;
19 
20     @Override
21     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
22             throws ServletException, IOException {
23         doPost(req, resp);
24     }
25 
26     @Override
27     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
28             throws ServletException, IOException {
29         req.setCharacterEncoding("utf-8");
30         resp.setHeader("Content-Type", "text/html");
31         String rootPath =req.getSession().getServletContext().getRealPath("/");
32         String jsonFilePath=rootPath+"/resources/ueditor/jsp/config.json";
33         resp.getWriter().write(new ActionEnter(req, rootPath,jsonFilePath).exec());
34     }
35 }
View Code

web.xml添加配置:

1     <servlet>
2         <servlet-name>ueditor</servlet-name>
3         <servlet-class>com.*.*.*.controller.UeditorController</servlet-class>
4     </servlet>
5     <servlet-mapping>
6         <servlet-name>ueditor</servlet-name>
7         <url-pattern>/ueditor/upload</url-pattern>
8     </servlet-mapping>
View Code

終於要結束了嗎?no,前端沒法顯示圖片,沒法在線管理,原來圖片上傳時保存的路徑是在config.json中的

1  "imagePathFormat": "/resources/ueditor/jsp/upload/image/{yyyy}{MM}{dd}/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */

而列出路徑是這樣的:

1 "imageManagerListPath": "/ueditor/jsp/upload/image/", /* 指定要列出圖片的目錄 */

這樣上面保存圖片時分日期保存的,而後參照列出路徑列出的是目錄,那確定不行的,因而乎幹掉imagePathFormat的日期,也就是{yyyy}{MM}{dd},變成這樣:

1  "imagePathFormat": "/resources/ueditor/jsp/upload/image/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */

改好後仍是不行,緣由是fileManagerUrlPrefix,imageManagerUrlPrefix配置有問題,最後改爲這樣終於正常了:

 1 /* 先後端通訊相關的配置,註釋只容許使用多行方式 */
 2 {
 3     /* 上傳圖片配置項 */
 4     "imageActionName": "uploadimage", /* 執行上傳圖片的action名稱 */
 5     "imageFieldName": "upfile", /* 提交的圖片表單名稱 */
 6     "imageMaxSize": 2048000, /* 上傳大小限制,單位B */
 7     "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上傳圖片格式顯示 */
 8     "imageCompressEnable": true, /* 是否壓縮圖片,默認是true */
 9     "imageCompressBorder": 1600, /* 圖片壓縮最長邊限制 */
10     "imageInsertAlign": "none", /* 插入的圖片浮動方式 */
11     "imageUrlPrefix": "/projectTest/", /* 圖片訪問路徑前綴 */
12     "imagePathFormat": "/resources/ueditor/jsp/upload/image/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */
13                                 /* {filename} 會替換成原文件名,配置這項須要注意中文亂碼問題 */
14                                 /* {rand:6} 會替換成隨機數,後面的數字是隨機數的位數 */
15                                 /* {time} 會替換成時間戳 */
16                                 /* {yyyy} 會替換成四位年份 */
17                                 /* {yy} 會替換成兩位年份 */
18                                 /* {mm} 會替換成兩位月份 */
19                                 /* {dd} 會替換成兩位日期 */
20                                 /* {hh} 會替換成兩位小時 */
21                                 /* {ii} 會替換成兩位分鐘 */
22                                 /* {ss} 會替換成兩位秒 */
23                                 /* 非法字符 \ : * ? " < > | */
24                                 /* 具請體看線上文檔: fex.baidu.com/ueditor/#use-format_upload_filename */
25 
26     /* 塗鴉圖片上傳配置項 */
27     "scrawlActionName": "uploadscrawl", /* 執行上傳塗鴉的action名稱 */
28     "scrawlFieldName": "upfile", /* 提交的圖片表單名稱 */
29     "scrawlPathFormat": "/resources/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */
30     "scrawlMaxSize": 2048000, /* 上傳大小限制,單位B */
31     "scrawlUrlPrefix": "/projectTest/", /* 圖片訪問路徑前綴 */
32     "scrawlInsertAlign": "none",
33 
34     /* 截圖工具上傳 */
35     "snapscreenActionName": "uploadimage", /* 執行上傳截圖的action名稱 */
36     "snapscreenPathFormat": "/resources/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */
37     "snapscreenUrlPrefix": "/projectTest/", /* 圖片訪問路徑前綴 */
38     "snapscreenInsertAlign": "none", /* 插入的圖片浮動方式 */
39 
40     /* 抓取遠程圖片配置 */
41     "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],
42     "catcherActionName": "catchimage", /* 執行抓取遠程圖片的action名稱 */
43     "catcherFieldName": "source", /* 提交的圖片列表表單名稱 */
44     "catcherPathFormat": "/resources/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */
45     "catcherUrlPrefix": "/projectTest/", /* 圖片訪問路徑前綴 */
46     "catcherMaxSize": 2048000, /* 上傳大小限制,單位B */
47     "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 抓取圖片格式顯示 */
48 
49     /* 上傳視頻配置 */
50     "videoActionName": "uploadvideo", /* 執行上傳視頻的action名稱 */
51     "videoFieldName": "upfile", /* 提交的視頻表單名稱 */
52     "videoPathFormat": "/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */
53     "videoUrlPrefix": "/projectTest/", /* 視頻訪問路徑前綴 */
54     "videoMaxSize": 102400000, /* 上傳大小限制,單位B,默認100MB */
55     "videoAllowFiles": [
56         ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
57         ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], /* 上傳視頻格式顯示 */
58 
59     /* 上傳文件配置 */
60     "fileActionName": "uploadfile", /* controller裏,執行上傳視頻的action名稱 */
61     "fileFieldName": "upfile", /* 提交的文件表單名稱 */
62     "filePathFormat": "/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */
63     "fileUrlPrefix": "/ringtione/", /* 文件訪問路徑前綴 */
64     "fileMaxSize": 51200000, /* 上傳大小限制,單位B,默認50MB */
65     "fileAllowFiles": [
66         ".png", ".jpg", ".jpeg", ".gif", ".bmp",
67         ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
68         ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
69         ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
70         ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
71     ], /* 上傳文件格式顯示 */
72 
73     /* 列出指定目錄下的圖片 */
74     "imageManagerActionName": "listimage", /* 執行圖片管理的action名稱 */
75     "imageManagerListPath": "/resources/ueditor/jsp/upload/image/", /* 指定要列出圖片的目錄 */
76     "imageManagerListSize": 20, /* 每次列出文件數量 */
77     "imageManagerUrlPrefix": "/projectTest/", /* 圖片訪問路徑前綴 */
78     "imageManagerInsertAlign": "none", /* 插入的圖片浮動方式 */
79     "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 列出的文件類型 */
80 
81     /* 列出指定目錄下的文件 */
82     "fileManagerActionName": "listfile", /* 執行文件管理的action名稱 */
83     "fileManagerListPath": "/resources/ueditor/jsp/upload/file/", /* 指定要列出文件的目錄 */
84     "fileManagerUrlPrefix": "/projectTest/", /* 文件訪問路徑前綴 */
85     "fileManagerListSize": 20, /* 每次列出文件數量 */
86     "fileManagerAllowFiles": [
87         ".png", ".jpg", ".jpeg", ".gif", ".bmp",
88         ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
89         ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
90         ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
91         ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
92     ] /* 列出的文件類型 */
93 
94 }
View Code

無法上傳文件,在把ConfigManager貼一下:

  1 package com.baidu.ueditor;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileNotFoundException;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 import java.io.UnsupportedEncodingException;
 10 import java.util.HashMap;
 11 import java.util.Map;
 12 
 13 import javax.servlet.http.HttpServletRequest;
 14 
 15 import org.json.JSONArray;
 16 import org.json.JSONObject;
 17 
 18 import com.baidu.ueditor.define.ActionMap;
 19 
 20 /**
 21  * 配置管理器
 22  * 
 23  * @author hancong03@baidu.com
 24  * 
 25  */
 26 public final class ConfigManager {
 27 
 28     private final String rootPath;
 29     private final String originalPath;
 30     private static final String configFileName = "config.json";
 31     private String parentPath = null;
 32     private JSONObject jsonConfig = null;
 33     // 塗鴉上傳filename定義
 34     private final static String SCRAWL_FILE_NAME = "scrawl";
 35     // 遠程圖片抓取filename定義
 36     private final static String REMOTE_FILE_NAME = "remote";
 37 
 38     /*
 39      * 經過一個給定的路徑構建一個配置管理器, 該管理器要求地址路徑所在目錄下必須存在config.properties文件
 40      */
 41     private ConfigManager(String rootPath,String jsonConfigFilePath, String contextPath, String uri)
 42             throws FileNotFoundException, IOException {
 43 
 44         rootPath = rootPath.replace("\\", "/");
 45 
 46         this.rootPath = rootPath;
 47 
 48         this.originalPath = this.rootPath + uri;
 49         
 50         this.parentPath=new File(jsonConfigFilePath).getParent();
 51         
 52         this.initEnv();
 53     }
 54 
 55     /**
 56      * 配置管理器構造工廠
 57      * 
 58      * @param rootPath
 59      *            服務器根路徑
 60      * @param contextPath
 61      *            服務器所在項目路徑
 62      * @param uri
 63      *            當前訪問的uri
 64      * @param jsonConfigFilePath
 65      *          json配置文件的絕對路徑,和controller.jsp在同一目錄
 66      * @return 配置管理器實例或者null
 67      */
 68     public static ConfigManager getInstance(String rootPath,String jsonConfigFilePath,
 69             String contextPath, String uri) {
 70 
 71         try {
 72             return new ConfigManager(rootPath,jsonConfigFilePath, contextPath, uri);
 73         } catch (Exception e) {
 74             return null;
 75         }
 76 
 77     }
 78 
 79     // 驗證配置文件加載是否正確
 80     public boolean valid() {
 81         return this.jsonConfig != null;
 82     }
 83 
 84     public JSONObject getAllConfig() {
 85 
 86         return this.jsonConfig;
 87 
 88     }
 89 
 90     public Map<String, Object> getConfig(int type) {
 91 
 92         Map<String, Object> conf = new HashMap<String, Object>();
 93         String savePath = null;
 94 
 95         switch (type) {
 96 
 97         case ActionMap.UPLOAD_FILE:
 98             conf.put("isBase64", "false");
 99             conf.put("maxSize", this.jsonConfig.getLong("fileMaxSize"));
100             conf.put("allowFiles", this.getArray("fileAllowFiles"));
101             conf.put("fieldName", this.jsonConfig.getString("fileFieldName"));
102             savePath = this.jsonConfig.getString("filePathFormat");
103             break;
104 
105         case ActionMap.UPLOAD_IMAGE:
106             conf.put("isBase64", "false");
107             conf.put("maxSize", this.jsonConfig.getLong("imageMaxSize"));
108             conf.put("allowFiles", this.getArray("imageAllowFiles"));
109             conf.put("fieldName", this.jsonConfig.getString("imageFieldName"));
110             savePath = this.jsonConfig.getString("imagePathFormat");
111             break;
112 
113         case ActionMap.UPLOAD_VIDEO:
114             conf.put("maxSize", this.jsonConfig.getLong("videoMaxSize"));
115             conf.put("allowFiles", this.getArray("videoAllowFiles"));
116             conf.put("fieldName", this.jsonConfig.getString("videoFieldName"));
117             savePath = this.jsonConfig.getString("videoPathFormat");
118             break;
119 
120         case ActionMap.UPLOAD_SCRAWL:
121             conf.put("filename", ConfigManager.SCRAWL_FILE_NAME);
122             conf.put("maxSize", this.jsonConfig.getLong("scrawlMaxSize"));
123             conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));
124             conf.put("isBase64", "true");
125             savePath = this.jsonConfig.getString("scrawlPathFormat");
126             break;
127 
128         case ActionMap.CATCH_IMAGE:
129             conf.put("filename", ConfigManager.REMOTE_FILE_NAME);
130             conf.put("filter", this.getArray("catcherLocalDomain"));
131             conf.put("maxSize", this.jsonConfig.getLong("catcherMaxSize"));
132             conf.put("allowFiles", this.getArray("catcherAllowFiles"));
133             conf.put("fieldName", this.jsonConfig.getString("catcherFieldName")
134                     + "[]");
135             savePath = this.jsonConfig.getString("catcherPathFormat");
136             break;
137 
138         case ActionMap.LIST_IMAGE:
139             conf.put("allowFiles", this.getArray("imageManagerAllowFiles"));
140             conf.put("dir", this.jsonConfig.getString("imageManagerListPath"));
141             conf.put("count", this.jsonConfig.getInt("imageManagerListSize"));
142             break;
143 
144         case ActionMap.LIST_FILE:
145             conf.put("allowFiles", this.getArray("fileManagerAllowFiles"));
146             conf.put("dir", this.jsonConfig.getString("fileManagerListPath"));
147             conf.put("count", this.jsonConfig.getInt("fileManagerListSize"));
148             break;
149 
150         }
151 
152         conf.put("savePath", savePath);
153         conf.put("rootPath", this.rootPath);
154 
155         return conf;
156 
157     }
158 
159     /**
160      * Get rootPath from request,if not,find it from conf map.
161      * 
162      * @param request
163      * @param conf
164      * @return
165      * @author Ternence
166      * @create 2015年1月31日
167      */
168     public static String getRootPath(HttpServletRequest request,
169             Map<String, Object> conf) {
170         Object rootPath = request.getAttribute("rootPath");
171         if (rootPath != null) {
172             return rootPath + "" + File.separatorChar;
173         } else {
174             return conf.get("rootPath") + "";
175         }
176     }
177 
178     private void initEnv() throws FileNotFoundException, IOException {
179 
180         File file = new File(this.originalPath);
181 
182         if (!file.isAbsolute()) {
183             file = new File(file.getAbsolutePath());
184         }
185 
186         if(this.parentPath==null)
187             this.parentPath = file.getParent();
188 
189         String configContent = this.readFile(this.getConfigPath());
190 
191         try {
192             JSONObject jsonConfig = new JSONObject(configContent);
193             this.jsonConfig = jsonConfig;
194         } catch (Exception e) {
195             this.jsonConfig = null;
196         }
197 
198     }
199 
200     private String getConfigPath() {
201         String path = this.getClass().getResource("/").getPath()
202                 + ConfigManager.configFileName;
203         if (new File(path).exists()) {
204             return path;
205         } else {
206             return this.parentPath + File.separator
207                     + ConfigManager.configFileName;
208         }
209     }
210 
211     private String[] getArray(String key) {
212 
213         JSONArray jsonArray = this.jsonConfig.getJSONArray(key);
214         String[] result = new String[jsonArray.length()];
215 
216         for (int i = 0, len = jsonArray.length(); i < len; i++) {
217             result[i] = jsonArray.getString(i);
218         }
219 
220         return result;
221 
222     }
223 
224     private String readFile(String path) throws IOException {
225 
226         StringBuilder builder = new StringBuilder();
227 
228         try {
229 
230             InputStreamReader reader = new InputStreamReader(
231                     new FileInputStream(path), "UTF-8");
232             BufferedReader bfReader = new BufferedReader(reader);
233 
234             String tmpContent = null;
235 
236             while ((tmpContent = bfReader.readLine()) != null) {
237                 builder.append(tmpContent);
238             }
239 
240             bfReader.close();
241 
242         } catch (UnsupportedEncodingException e) {
243             // 忽略
244         }
245 
246         return this.filter(builder.toString());
247 
248     }
249 
250     // 過濾輸入字符串, 剔除多行註釋以及替換掉反斜槓
251     private String filter(String input) {
252 
253         return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
254 
255     }
256 
257 }
View Code

 

最後在給個github ue的url:https://github.com/fex-team/ueditor,將ActionEnter和ConfigManager替換並導出的jar替換官方的jar便可按照圖中所述的方式使用了。

相關文章
相關標籤/搜索