Java web 引入wangEditor編輯器

來源:互聯網
上載者:User

標籤:flash外掛程式   hooks   esc   不能   原理   conf   字元   change   display   

最近項目需要我做些前端的事,這讓我這個半吊子前端很是痛苦。項目中需要一個編輯器,之前找了個ueditor,外掛程式比較多,需要改源碼等等不說,最後弄好了,發現需要flash外掛程式。沒辦法,只有推到重來,在網上尋找對比後,最後決定使用用wangEditor,首先它簡單,輕量,基本能滿足我的需求,不好的地方就是網上的demo不是很多,不是大公司的開源產品,不知道能不能堅持更新下去,不過ueditor都不更新了,也沒必要強求。

首先貼上官方地址:https://www.kancloud.cn/wangfupeng/wangeditor3/335771。

沒什麼說的,就是簡潔,輕量、官網上的例子已經寫很詳細了,主要記錄下我的做法。

 <div id="div1">        </div>    <textarea name="ueditorContent" id="ueditorContent" style="width:100%; height:200px;display:none" ></textarea>    <script type="text/javascript">        var E = window.wangEditor;        var editor = new E(‘#div1‘);        var $ueditorContent = $(‘#ueditorContent‘);        editor.customConfig.onchange = function (html) {            // 監控變化,同步更新到 textarea            $ueditorContent.val(html);        };        editor.customConfig.uploadImgServer = ‘/bhym/systemController/fileUp.do‘ ; // 上傳圖片到伺服器,        // 隱藏“網狀圖片”tab       // editor.customConfig.showLinkImg = false;        editor.customConfig.uploadImgHooks = {                // (但是,伺服器端返回的必須是一個 JSON 格式字串!!!否則會報錯)                customInsert: function (insertImg, result, editor) {                    // insertImg 是插入圖片的函數,editor 是編輯器對象,result 是伺服器端返回的結果:                    var url = result.obj;                    insertImg(url);                },              },        editor.create();        // 初始化 textarea 的值,向後台提交textarea中的值        $ueditorContent.val(editor.txt.html())    </script>

因為前端參數比較多,不想單獨去寫個ajax請求去發送請求。所以為了偷下懶,就在編輯器的下方,定義了一個textarea ,將它隱藏掉,將編輯器的內容同步更新到textarea中。再通過將textarea中的內容用提交form

表單的方式提交。前端就這樣了,再來看看背景圖片處理,思路是:按照常規的檔案上傳方式將圖片上傳到伺服器,返回一個url(虛擬路徑)再通過配置虛擬路徑的方式訪問圖片,前面有篇文章說的怎麼配置虛擬路徑:http://www.cnblogs.com/magic101/p/7756402.html。

/**     *      * @Title: fileUp     * @Description:wangEditor上傳圖片     * @param     * @return     */    @RequestMapping("/fileUp")    @ResponseBody    public AjaxJson fileUp(HttpServletRequest request) {        AjaxJson j = new AjaxJson();        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;        try {            multipartRequest.setCharacterEncoding("UTF-8");            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();            // 檔案資料庫儲存的路徑            String path = null;            // 檔案儲存在硬碟的相對路徑            String realPath = null;            realPath = ResourceUtil.getConfigByName("webUploadpath") + "/";            path = ResourceUtil.getConfigByName("http_url") + "/";            File file = new File(realPath);            if (!file.exists()) {                file.mkdirs();// 建立根目錄            }            realPath += DateUtils.getDataString(DateUtils.yyyyMMdd) + "/";            path += DateUtils.getDataString(DateUtils.yyyyMMdd) + "/";            file = new File(realPath);            if (!file.exists()) {                file.mkdir();// 建立檔案時間子目錄            }            String fileName = "";            // String swfName = "";            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {                MultipartFile mf = entity.getValue();// 擷取上傳檔案對象                fileName = mf.getOriginalFilename();// 擷取檔案名稱                // swfName =                // PinyinUtil.getPinYinHeadChar(oConvertUtils.replaceBlank(FileUtils.getFilePrefix(fileName)));//                // 取檔案名稱首字母作為SWF檔案名稱                String extend = FileUtils.getExtend(fileName);// 擷取副檔名                String myfilename = "";                String noextfilename = "";// 不帶副檔名                noextfilename = DateUtils.getDataString(DateUtils.yyyymmddhhmmss) + StringUtil.random(8);// 自訂檔案名稱                myfilename = noextfilename + "." + extend;// 自訂檔案名稱                String savePath = realPath + myfilename;// 檔案儲存全路徑                File savefile = new File(savePath);                if (entity.getKey() != null) {                    // 設定檔案資料庫的實體路徑                    String filePath = path + myfilename;                    j.setObj(filePath);                }                // 檔案拷貝到指定硬碟目錄                if ("txt".equals(extend)) {                    // 利用utf-8字元集的固定首行隱藏編碼原理                    // Unicode:FF FE UTF-8:EF BB                    byte[] allbytes = mf.getBytes();                    try {                        String head1 = toHexString(allbytes[0]);                        // System.out.println(head1);                        String head2 = toHexString(allbytes[1]);                        // System.out.println(head2);                        if ("ef".equals(head1) && "bb".equals(head2)) {                            // UTF-8                            String contents = new String(mf.getBytes(), "UTF-8");                            if (StringUtils.isNotBlank(contents)) {                                OutputStream out = new FileOutputStream(savePath);                                out.write(contents.getBytes());                                out.close();                            }                        } else {                            // GBK                            String contents = new String(mf.getBytes(), "GBK");                            OutputStream out = new FileOutputStream(savePath);                            out.write(contents.getBytes());                            out.close();                        }                    } catch (Exception e) {                        String contents = new String(mf.getBytes(), "UTF-8");                        if (StringUtils.isNotBlank(contents)) {                            OutputStream out = new FileOutputStream(savePath);                            out.write(contents.getBytes());                            out.close();                        }                    }                } else {                    FileCopyUtils.copy(mf.getBytes(), savefile);                }            }        } catch (Exception e) {            j.setSuccess(false);            e.printStackTrace();        }        return j;    }    private String toHexString(int index) {        String hexString = Integer.toHexString(index);        // 1個byte變成16進位的,只需要2位就可以表示了,取後面兩位,去掉前面的符號填充        hexString = hexString.substring(hexString.length() - 2);        return hexString;    }

最後再來測試一下:

然後再訪問一下資料庫。發現確實也存入了的。

 

Java web 引入wangEditor編輯器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.