欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现在线预览的示例代码(openOffice实现)

程序员文章站 2024-02-26 11:36:04
简介 之前有写了poi实现在线预览的文章,里面也说到了使用openoffice也可以做到,这里就详细介绍一下。 我的实现逻辑有两种: 一、利用jodcon...

简介

之前有写了poi实现在线预览的文章,里面也说到了使用openoffice也可以做到,这里就详细介绍一下。

我的实现逻辑有两种:

一、利用jodconverter(基于openoffice服务)将文件(.doc、.docx、.xls、.ppt)转化为html格式。

二、利用jodconverter(基于openoffice服务)将文件(.doc、.docx、.xls、.ppt)转化为pdf格式。

转换成html格式大家都能理解,这样就可以直接在浏览器上查看了,也就实现了在线预览的功能;转换成pdf格式这点,需要用户安装了adobe reader xi,这样你会发现把pdf直接拖到浏览器页面可以直接打开预览,这样也就实现了在线预览的功能。

将文件转化为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服务)将文件(*.doc、*.docx、*.xls、*.ppt)转化为html格式或者pdf格式,
 * 使用前请检查openoffice服务是否已经开启, openoffice进程名称: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;
  }

  /**
   * 转换文件成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构建输入文件
     */
    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("文件转换出错,请检查openoffice服务是否启动。");
    }
    // convert
    documentconverter converter = new openofficedocumentconverter(connection);
    converter.convert(docinputfile, htmloutputfile);
    connection.disconnect();
    // 转换完之后删除word文件
    docinputfile.delete();
    return htmfilename;
  }

  /**
   * 转换文件成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构建输入文件
     */
    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("文件转换出错,请检查openoffice服务是否启动。");
    }
    // convert
    documentconverter converter = new openofficedocumentconverter(connection);
    converter.convert(docinputfile, htmloutputfile);
    connection.disconnect();
    // 转换完之后删除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");

  }

}

转换成html和转换成pdf的过程几乎一样,只是在创建输出的file时前者命名为xxx.html,后者命名为xxx.pdf,在执行converter.convert(docinputfile, htmloutputfile);时,jodconverter会自己根据文件类型名转换成对应的文件。

注意,main方法里别file2html和file2pdf都调用,会报错的,要么转html,要么转pdf,只能选一个。还有就是在执行之前,需要启动openoffice的服务:在openoffice目录下的命令窗口中执行soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard即可启动。

以上需要引入jodconverter的jar包。希望对大家的学习有所帮助,也希望大家多多支持。