ueditor 添加支持文件存儲到文件服務器

1、環境

ueditor.jar版本 爲1.1.2

2、場景

1、在webapp開發中,需要將富文本中的文件存儲到單獨的文件服務器,而ueditor默認只能在web項目根路徑下。
2、ueditor生成的富文本里圖片路徑是相對路徑,因而在app端無法顯示

3、快速解決方法

4、解決步驟

1、從官網下載jsp版本和完整源碼 http://ueditor.baidu.com/website/download.html

2、按官方文檔將jsp項目導入到eclipse或iead中並正常運行 http://fex.baidu.com/ueditor/#server-deploy

3、複製源碼目錄中\jsp\src\到iead項目源碼中、並把ueditor-xxx.jar依賴去掉

此時的項目結構:
此時的項目結構

4、ueditor-xxx.jar的源碼就不解析了。代碼不多。大家大致看下就能懂了。

5、修改方法com.baidu.ueditor.ConfigManager.getConfig(int type);
在最加入
conf.put(“externalStoragePath”, this.jsonConfig.optString(“externalStoragePath”));

如下
這裏寫圖片描述

6、在
com.baidu.ueditor.upload.BinaryUploader類中,添加如下方法取文件存儲位置

    private static String getPhysicalPath(Map<String, Object> conf, String savePath, String rootPath) {
        String externalStoragePath = (String) conf.get("externalStoragePath");
        String physicalPath;
        if (externalStoragePath != null && externalStoragePath.trim().length() != 0) {
            physicalPath = externalStoragePath;
        } else {
            physicalPath = rootPath;
        }
        physicalPath += savePath;
        return physicalPath;
    }
將該類save(HttpServletRequest request,Map<String, Object> conf)方法中的
    String physicalPath = rootPath + savePath;
改成
    String physicalPath = getPhysicalPath(conf, savePath, rootPath);

7、將com.baidu.ueditor.upload.Base64Uploader類按上述修改方式進行同樣的修改。

8、大功告成

9、在ueditor/jsp/config配置中添加

"externalStoragePath":"/opt/fileService/img/",/*文件服務器路徑*/

"imageUrlPrefix": "http://xxx.xxx.xxx:8080/xxx/xxx", /* 圖片訪問路徑前綴-要文件服務器的外網地址 */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章