欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)在線預(yù)覽的示例代碼(openOffice實現(xiàn))

 更新時間:2017年11月29日 11:46:56   作者:yjclsx  
本篇文章主要介紹了Java實現(xiàn)在線預(yù)覽的示例代碼(openOffice實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

簡介

之前有寫了poi實現(xiàn)在線預(yù)覽的文章,里面也說到了使用openOffice也可以做到,這里就詳細(xì)介紹一下。

我的實現(xiàn)邏輯有兩種:

一、利用jodconverter(基于OpenOffice服務(wù))將文件(.doc、.docx、.xls、.ppt)轉(zhuǎn)化為html格式。

二、利用jodconverter(基于OpenOffice服務(wù))將文件(.doc、.docx、.xls、.ppt)轉(zhuǎn)化為pdf格式。

轉(zhuǎn)換成html格式大家都能理解,這樣就可以直接在瀏覽器上查看了,也就實現(xiàn)了在線預(yù)覽的功能;轉(zhuǎn)換成pdf格式這點,需要用戶安裝了Adobe Reader XI,這樣你會發(fā)現(xiàn)把pdf直接拖到瀏覽器頁面可以直接打開預(yù)覽,這樣也就實現(xiàn)了在線預(yù)覽的功能。

將文件轉(zhuǎn)化為html格式或者pdf格式

話不多說,直接上代碼。

package com.pdfPreview.util;

import java.io.File;
import java.io.FileInputStream;
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 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;
/**
 * 利用jodconverter(基于OpenOffice服務(wù))將文件(*.doc、*.docx、*.xls、*.ppt)轉(zhuǎn)化為html格式或者pdf格式,
 * 使用前請檢查OpenOffice服務(wù)是否已經(jīng)開啟, OpenOffice進(jìn)程名稱:soffice.exe | soffice.bin
 * 
 * @author yjclsx
 */
public class Doc2HtmlUtil {

  private static Doc2HtmlUtil doc2HtmlUtil;

  /**
   * 獲取Doc2HtmlUtil實例
   */
  public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
    if (doc2HtmlUtil == null) {
      doc2HtmlUtil = new Doc2HtmlUtil();
    }
    return doc2HtmlUtil;
  }

  /**
   * 轉(zhuǎn)換文件成html
   * 
   * @param fromFileInputStream:
   * @throws IOException 
   */
  public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String timesuffix = sdf.format(date);
    String docFileName = null;
    String htmFileName = null;
    if("doc".equals(type)){
      docFileName = "doc_" + timesuffix + ".doc";
      htmFileName = "doc_" + timesuffix + ".html";
    }else if("docx".equals(type)){
      docFileName = "docx_" + timesuffix + ".docx";
      htmFileName = "docx_" + timesuffix + ".html";
    }else if("xls".equals(type)){
      docFileName = "xls_" + timesuffix + ".xls";
      htmFileName = "xls_" + timesuffix + ".html";
    }else if("ppt".equals(type)){
      docFileName = "ppt_" + timesuffix + ".ppt";
      htmFileName = "ppt_" + timesuffix + ".html";
    }else{
      return null;
    }

    File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
    File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
    if (htmlOutputFile.exists())
      htmlOutputFile.delete();
    htmlOutputFile.createNewFile();
    if (docInputFile.exists())
      docInputFile.delete();
    docInputFile.createNewFile();
    /**
     * 由fromFileInputStream構(gòu)建輸入文件
     */
    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) {
    }

    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
    try {
      connection.connect();
    } catch (ConnectException e) {
      System.err.println("文件轉(zhuǎn)換出錯,請檢查OpenOffice服務(wù)是否啟動。");
    }
    // convert
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    converter.convert(docInputFile, htmlOutputFile);
    connection.disconnect();
    // 轉(zhuǎn)換完之后刪除word文件
    docInputFile.delete();
    return htmFileName;
  }

  /**
   * 轉(zhuǎn)換文件成pdf
   * 
   * @param fromFileInputStream:
   * @throws IOException 
   */
  public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String timesuffix = sdf.format(date);
    String docFileName = null;
    String htmFileName = null;
    if("doc".equals(type)){
      docFileName = "doc_" + timesuffix + ".doc";
      htmFileName = "doc_" + timesuffix + ".pdf";
    }else if("docx".equals(type)){
      docFileName = "docx_" + timesuffix + ".docx";
      htmFileName = "docx_" + timesuffix + ".pdf";
    }else if("xls".equals(type)){
      docFileName = "xls_" + timesuffix + ".xls";
      htmFileName = "xls_" + timesuffix + ".pdf";
    }else if("ppt".equals(type)){
      docFileName = "ppt_" + timesuffix + ".ppt";
      htmFileName = "ppt_" + timesuffix + ".pdf";
    }else{
      return null;
    }

    File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
    File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
    if (htmlOutputFile.exists())
      htmlOutputFile.delete();
    htmlOutputFile.createNewFile();
    if (docInputFile.exists())
      docInputFile.delete();
    docInputFile.createNewFile();
    /**
     * 由fromFileInputStream構(gòu)建輸入文件
     */
    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) {
    }

    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
    try {
      connection.connect();
    } catch (ConnectException e) {
      System.err.println("文件轉(zhuǎn)換出錯,請檢查OpenOffice服務(wù)是否啟動。");
    }
    // convert
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    converter.convert(docInputFile, htmlOutputFile);
    connection.disconnect();
    // 轉(zhuǎn)換完之后刪除word文件
    docInputFile.delete();
    return htmFileName;
  }

  public static void main(String[] args) throws IOException {
    Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
    File file = null;
    FileInputStream fileInputStream = null;

    file = new File("D:/poi-test/exportExcel.xls");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls");

    file = new File("D:/poi-test/test.doc");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc","doc");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/doc","doc");

    file = new File("D:/poi-test/周報模版.ppt");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");

    file = new File("D:/poi-test/test.docx");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx");

  }

}

轉(zhuǎn)換成html和轉(zhuǎn)換成pdf的過程幾乎一樣,只是在創(chuàng)建輸出的File時前者命名為XXX.html,后者命名為XXX.pdf,在執(zhí)行converter.convert(docInputFile, htmlOutputFile);時,jodconverter會自己根據(jù)文件類型名轉(zhuǎn)換成對應(yīng)的文件。

注意,main方法里別file2Html和file2pdf都調(diào)用,會報錯的,要么轉(zhuǎn)html,要么轉(zhuǎn)pdf,只能選一個。還有就是在執(zhí)行之前,需要啟動openOffice的服務(wù):在openOffice目錄下的命令窗口中執(zhí)行soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard即可啟動。

以上需要引入jodconverter的jar包。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中ArrayList的兩種排序方法實例

    java中ArrayList的兩種排序方法實例

    ArrayList是一個數(shù)組隊列,相當(dāng)于 動態(tài)數(shù)組,與Java中的數(shù)組相比,它的容量能動態(tài)增長,這篇文章主要給大家介紹了關(guān)于java中ArrayList的兩種排序方法,需要的朋友可以參考下
    2021-07-07
  • Java IO創(chuàng)建目錄和文件實例代碼

    Java IO創(chuàng)建目錄和文件實例代碼

    本篇文章給大家分享了Java IO創(chuàng)建目錄和文件的實例代碼,過程很簡單,大家可以測試參考下。
    2018-02-02
  • springBoot整合CXF并實現(xiàn)用戶名密碼校驗的方法

    springBoot整合CXF并實現(xiàn)用戶名密碼校驗的方法

    這篇文章主要介紹了springBoot整合CXF并實現(xiàn)用戶名密碼校驗的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • java開發(fā)AOP面向切面編程入門

    java開發(fā)AOP面向切面編程入門

    這篇文章主要介紹了java開發(fā)的AOP面向切面編程入門的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步學(xué)有所得
    2021-10-10
  • Spring?Lifecycle的使用小結(jié)

    Spring?Lifecycle的使用小結(jié)

    這篇文章主要介紹了Spring?Lifecycle的使用,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)詳解

    Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)詳解

    所有負(fù)載均衡的場景幾乎都會用到這個平滑加權(quán)輪詢算法,下面這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • MybatisPlus中的insert操作詳解

    MybatisPlus中的insert操作詳解

    這篇文章主要介紹了MybatisPlus中的insert操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例詳解

    java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例詳解

    在本篇文章里小編給大家分享的是關(guān)于java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-05-05
  • Java Swing組件定制RadioButton示例

    Java Swing組件定制RadioButton示例

    這篇文章主要介紹了Java Swing組件定制RadioButton,結(jié)合實例形式分析了java swing組件RadioButton相關(guān)屬性設(shè)置與操作技巧,需要的朋友可以參考下
    2018-01-01
  • Java實現(xiàn)簡易生產(chǎn)者消費者模型過程解析

    Java實現(xiàn)簡易生產(chǎn)者消費者模型過程解析

    這篇文章主要介紹了Java實現(xiàn)簡易生產(chǎn)者消費者模型過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06

最新評論