借用OpenOffice將上傳的Word文檔轉換成Html格式
有個博友寫的比較詳細,參考地址:https://www.cnblogs.com/luckyxiaoxuan/archive/2012/06/13/2548331.html
將Word轉Html的原理是這樣的:
1、客戶上傳Word文檔到服務器
2、服務器調用OpenOffice程序打開上傳的Word文檔
3、OpenOffice將Word文檔另存為Html格式
4、Over
至此可見,這要求服務器端安裝OpenOffice軟件,其實也可以是MS Office,不過OpenOffice的優勢是跨平台,你懂的。恩,說明一下,本文的測試基於 MS Win7 Ultimate X64 係統。
下麵就是規規矩矩的實現。
1、下載OpenOffice,https://download.openoffice.org/index.html So easy...
2、下載Jodconverter https://www.artofsolving.com/opensource/jodconverter 這是一個開啟OpenOffice進行格式轉化的第三方jar包。
3、泡杯熱茶,等待下載。
4、安裝OpenOffice,安裝結束後,調用cmd,啟動OpenOffice的一項服務:C:\Program Files (x86)\OpenOffice.org 3\program>soffice -headless -accept="socket,port=8100;urp;"
5、打開eclipse
6、喝杯熱茶,等待eclipse打開。
7、新建eclipse項目,導入Jodconverter/lib 下得jar包。
* commons-io
* jodconverter
* juh
* jurt
* ridl
* slf4j-api
* slf4j-jdk14
* unoil
* xstream
8、Coding...
查看代碼 package com.mzule.doc2html.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.net.ConnectException; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; /** * 將Word文檔轉換成html字符串的工具類 * * @author MZULE * */ public class Doc2Html { public static void main(String[] args) { System.out .println(toHtmlString(new File("C:/test/test.doc"), "C:/test")); } /** * 將word文檔轉換成html文檔 * * @param docFile * 需要轉換的word文檔 * @param filepath * 轉換之後html的存放路徑 * @return 轉換之後的html文件 */ public static File convert(File docFile, String filepath) { // 創建保存html的文件 File htmlFile = new File(filepath + "/" + new Date().getTime() + ".html"); // 創建Openoffice連接 OpenOfficeConnection con = new SocketOpenOfficeConnection(8100); try { // 連接 con.connect(); } catch (ConnectException e) { System.out.println("獲取OpenOffice連接失敗..."); e.printStackTrace(); } // 創建轉換器 DocumentConverter converter = new OpenOfficeDocumentConverter(con); // 轉換文檔問html converter.convert(docFile, htmlFile); // 關閉openoffice連接 con.disconnect(); return htmlFile; } /** * 將word轉換成html文件,並且獲取html文件代碼。 * * @param docFile * 需要轉換的文檔 * @param filepath * 文檔中圖片的保存位置 * @return 轉換成功的html代碼 */ public static String toHtmlString(File docFile, String filepath) { // 轉換word文檔 File htmlFile = convert(docFile, filepath); // 獲取html文件流 StringBuffer htmlSb = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(htmlFile))); while (br.ready()) { htmlSb.append(br.readLine()); } br.close(); // 刪除臨時文件 htmlFile.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // HTML文件字符串 String htmlStr = htmlSb.toString(); // 返回經過清潔的html文本 return clearFormat(htmlStr, filepath); } /** * 清除一些不需要的html標記 * * @param htmlStr * 帶有複雜html標記的html語句 * @return 去除了不需要html標記的語句 */ protected static String clearFormat(String htmlStr, String docImgPath) { // 獲取body內容的正則 String bodyReg = "<BODY .*</BODY>"; Pattern bodyPattern = Pattern.compile(bodyReg); Matcher bodyMatcher = bodyPattern.matcher(htmlStr); if (bodyMatcher.find()) { // 獲取BODY內容,並轉化BODY標簽為DIV htmlStr = bodyMatcher.group().replaceFirst("<BODY", "<DIV") .replaceAll("</BODY>", "</DIV>"); } // 調整圖片地址 htmlStr = htmlStr.replaceAll("<IMG SRC=\"", "<IMG SRC=\"" + docImgPath + "/"); // 把<P></P>轉換成</div></div>保留樣式 // content = content.replaceAll("(<P)([^>]*>.*?)(<\\/P>)", // "<div$2</div>"); // 把<P></P>轉換成</div></div>並刪除樣式 htmlStr = htmlStr.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>"); // 刪除不需要的標簽 htmlStr = htmlStr .replaceAll( "<[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^>]*?>", ""); // 刪除不需要的屬性 htmlStr = htmlStr .replaceAll( "<([^>]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>", "<$1$2>"); return htmlStr; } }
類組織的不好,博友湊合看,代碼注釋比較詳細了,不多說。
兩個公開的方法是獨立使用的,toHtmlString(...)方法是轉化文件並獲取html代碼,以備存入數據庫。
參考了https://dangry.iteye.com/blog/858787,表示感謝。最後更新:2017-04-03 12:55:33