在線編輯器(WangEditor)

  本身以前寫了一篇關於POI 相關的博客, 想了想在公司中通常經常使用的不就是上傳下載,poi,分頁,定時等。好像還有個在線編輯器, 因而本身就花了兩個多小時把編輯器相關的代碼擼了遍,固然了是先百度找了找資料,看了看實現的邏輯,而後本身擼的。 編輯器本身使用的是WangEditor,網上也有不少關於Editor,kindEitor 的文章, 不過貌似好像沒用。業務方面:在編輯器中編輯, 而後保存爲word,或者將word中的內容加載進在線編輯器中再次編輯。效果圖:html

 

    http://www.wangeditor.com/   這是WangEditor的相關網址,其中api,文檔,實例都有。 WangEditor使用,配置仍是相對來講比較簡單的,引入相關js,建立editor對象,初始化對象。java

    

    

  editor.txt.html() 會將在編輯器中編輯的內容獲取,而後你直接將其傳入後臺即可以獲取到編輯器中編輯的內容。api

  當你使用編輯器編輯並保存後,會在指定的保存位置生成一個word,txt文件夾和一天個htm文件。txt文件夾中是txt文件。txt文件和htm文件都是自動生成的。其中txt文件裏是HTML中的標籤語言,當你要將word中的內容加載進編輯器再次編輯時,獲取的內容是相對應的txt文件中的內容。htm文件只有一個,是剛使用用WangEditor建立word成功後生成的,其就是個HTML文件,其中的標籤,屬性對應的都是編輯器中展現的模樣。當你保存生成word時,是先讀取htm中的內容,將${content}替換成你編輯的內容,樣式什麼的htm文件中模板原先就有。而後利用流將HTML中的內容寫入到word中並生成word。瀏覽器

  

  

package com.cn.platform.utils;

import java.io.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EditorUtils {

    // 獲取項目文件路徑
public static String getUploadPath(HttpServletRequest request,String name){
      StringBuilder sb = new StringBuilder();
      String path = request.getContextPath();
      String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
      String uploadPath = sb.append(basePath).append("/ui/CAndTFiles/").append(name).append(".doc").toString();
      return uploadPath;
}

//獲取服務器,本地文件路徑
public static String getWindowsPath(HttpServletRequest request,String name){
    StringBuilder sb = new StringBuilder();
    String windowPath = sb.append("I:/yishangxincheng/ysdFiles/").append(name).append(".doc").toString();
    return windowPath;
}

//獲取服務器,本地文件路徑
public static String getWindowsTxtPath(HttpServletRequest request,String name){
    StringBuilder sb = new StringBuilder();
    String windowPath = sb.append("I:/yishangxincheng/ysdFiles/txt/").append(name).append(".txt").toString();
    return windowPath;
}

/*public static void saveWord(String editTemplate,String windowPath,HttpServletRequest request,HttpServletResponse response) throws IOException{
    EditorUtils.setCode(request, response);
    if (editTemplate != null) {
        List<String> array = new ArrayList<>();
        array.add(editTemplate);
        XWPFDocument doc = new XWPFDocument();  
        XWPFParagraph para = doc.createParagraph();  
        XWPFRun run = para.createRun();  
        OutputStream os = new FileOutputStream(windowPath);  
        for (String s : array) {
              //把doc輸出到輸出流 
              run.setText(s);
              doc.write(os);  
        }
        os.close();
        doc.close();
    }
}*/


//設置編碼
public static void setCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
}

//導出
public static void export(HttpServletRequest request,HttpServletResponse response,String url) throws IOException {
    EditorUtils.setCode(request, response);
    //獲取文件下載路徑
    String filename = url.substring(url.length()-4, url.length());
    if (filename.equals("docx")) {
        filename = url.substring(url.length()-6, url.length());
    }else{
        filename = url.substring(url.length()-5, url.length());
    }
    File file =  new File(url);
    if(file.exists()){
        //設置相應類型讓瀏覽器知道用什麼打開  用application/octet-stream也能夠,看是什麼瀏覽器
        response.setContentType("application/x-msdownload");
        //設置頭信息
        StringBuilder sb = new StringBuilder();
        response.setHeader("Content-Disposition", sb.append("attachment;filename=\"").append(filename).append("\"").toString());
        InputStream inputStream = new FileInputStream(file);
        ServletOutputStream ouputStream = response.getOutputStream();
        byte b[] = new byte[1024];
        int n ;
        while((n = inputStream.read(b)) != -1){
            ouputStream.write(b,0,n);
        }
        //關閉流
        ouputStream.close();
        inputStream.close();
    }
}


// 讀取.mht網頁中的信息
private  static String readFile(String filePath) throws Exception{
    StringBuilder sb = new StringBuilder();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"utf-8"));
        while (br.ready()) {
            sb.append((char) br.read());
        }
    }catch(Exception e){
        e.printStackTrace();
    }finally {
        if (br!=null) {
            br.close();
        }
    }
    return sb.toString();
}

 //將HTML轉word
   private static boolean writeWordFile(String content ,String path,String fileName) throws Exception{
          boolean flag = false;
          FileOutputStream fos = null;
          StringBuilder sb = new StringBuilder();
          try {
              if(!"".equals(path)){
                  byte[]b = content.getBytes("utf-8");
                  fos = new FileOutputStream(sb.append(path).append(fileName).append(".doc").toString());
                  fos.write(b);
                  fos.close();
                  flag = true;
              }
          }catch (IOException e)
          {
              e.printStackTrace();
          }finally {
              if (fos !=null) {
                  fos.close();
              }
          }
          return flag;
   }

   public static void htmlToWord(String editorContent,String htmlPath,HttpServletRequest request,String wordPath,String wordName) throws Exception{
       //讀取網頁中的內容
       String htmlFile = EditorUtils.readFile(htmlPath);  
       // 替換後的內容
       String endContent = htmlFile.replace("${content}", editorContent); 
       //轉word
       EditorUtils.writeWordFile(endContent, wordPath, wordName); 
   }
  
   // 將editorContent存入txt中用於載入時直接使用
   public static void saveEditorContent(String editorContent,String targetPath,String fileName) throws IOException{
       FileOutputStream fos = null;
       StringBuilder sb = new StringBuilder();
       try {
           if(!"".equals(targetPath)){
               byte[]b = editorContent.getBytes("utf-8");
               fos = new FileOutputStream(targetPath);
               fos.write(b);
               fos.close();
           }
       }catch (IOException e)
       {
           e.printStackTrace();
       }finally {
           if (fos !=null) {
               fos.close();
           }
       }
       
   }
   
 //載入
   public static String load(String name,HttpServletRequest request,HttpServletResponse response) throws IOException{
       EditorUtils.setCode(request, response);
       String path = EditorUtils.getWindowsTxtPath(request, name);
       StringBuilder sb= new StringBuilder();
       BufferedReader br = null;
       try {
           br = new BufferedReader(new InputStreamReader(new FileInputStream(path),"utf-8"));
           while (br.ready()) {
               sb.append((char) br.read());
           }
       }catch(Exception e){
           e.printStackTrace();
       }finally {
           if (br!=null) {
               br.close();
           }
       }
       
       return sb.toString();
   }
   

}

  其中主要的代碼就是工具類,代碼都是能直接使用的。固然了,代碼我還有10%沒弄上來,不過我相信有了這些代碼,看到此篇博客的人應該沒問題。服務器

  在此,但願此篇博客能幫助到一些人。有不足之處,有問題的話能夠博客上Q我,看到就會回覆app

相關文章
相關標籤/搜索