利用jodconverter(基於OpenOffice服務)將word檔案(*.doc)轉化為html格式

來源:互聯網
上載者:User
package com.syni.im800.kb.common.util;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ConnectException;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;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;import com.syni.im800.kb.config.AppConfig;/** * 利用jodconverter(基於OpenOffice服務)將word檔案(*.doc)轉化為html格式, * 使用前請檢查OpenOffice服務是否已經開啟, * OpenOffice進程名稱:soffice.exe | soffice.bin * @author linshutao * */public class Doc2HtmlUtil {Log log = LogFactory.getLog(getClass());private static Doc2HtmlUtil doc2HtmlUtil;/** * 擷取Doc2HtmlUtil執行個體 * */public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance(){if(doc2HtmlUtil == null){doc2HtmlUtil = new Doc2HtmlUtil();}return doc2HtmlUtil;}/** * 轉換檔 * @param fromFileInputStream:  * */public String doc2Html(InputStream fromFileInputStream, File toFileFolder){String soffice_host = AppConfig.getProperty(AppConfig.SOFFICE_HOST_KEY);String soffice_port = AppConfig.getProperty(AppConfig.SOFFICE_PORT_KEY);log.debug("soffice_host:"+soffice_host+",soffice_port:"+soffice_port);Date date = new Date();SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmmss");String timesuffix = sdf.format(date);String htmFileName = "htmlfile"+timesuffix+".html";String docFileName = "docfile"+timesuffix+".doc";File htmlOutputFile = new File(toFileFolder.toString()+File.separatorChar+htmFileName);File docInputFile = new File(toFileFolder.toString()+File.separatorChar+docFileName);log.debug("########htmlOutputFile:"+toFileFolder.toString()+File.pathSeparator+htmFileName);/** * 由fromFileInputStream構建輸入檔案 * */try {OutputStream os = new FileOutputStream(docInputFile);int bytesRead = 0; byte[] buffer = new byte[1024 * 8];while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {os.write(buffer, 0, bytesRead);}os.close();fromFileInputStream.close();} catch (IOException e) {log.error(e.getMessage(), e);}OpenOfficeConnection connection = new SocketOpenOfficeConnection(soffice_host,Integer.parseInt(soffice_port));try {connection.connect();} catch (ConnectException e) { System.err.println("檔案轉換出錯,請檢查OpenOffice服務是否啟動。");   log.error(e.getMessage(), e);}// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(docInputFile, htmlOutputFile);connection.disconnect();/*File  htmlOutputFile_rn = new File(htmlOutputFile.getAbsolutePath().substring(0,htmlOutputFile.getAbsolutePath().lastIndexOf("."))+".htm");htmlOutputFile.renameTo(htmlOutputFile_rn);return htmlOutputFile_rn.getName();*///轉換完之後刪除word檔案docInputFile.delete();log.debug("刪除上傳檔案:"+docInputFile.getName());return htmFileName;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.