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

 更新时间:2017年11月29日 11:46:56   作者:yjclsx  
本篇文章主要介绍了Java实现在线预览的示例代码(openOffice实现),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

简介

之前有写了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包。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • mybatis-plus多表联查join的实现

    mybatis-plus多表联查join的实现

    本文主要介绍了mybatis-plus多表联查join的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • Java实现Excel文件转PDF(无水印无限制)

    Java实现Excel文件转PDF(无水印无限制)

    这篇文章主要为大家详细介绍了如何利用Java语言实现Excel文件转PDF的效果,并可以无水印、无限制。文中的示例代码讲解详细,需要的可以参考一下
    2022-06-06
  • java DecimalFormat常用方法详解

    java DecimalFormat常用方法详解

    这篇文章主要为大家详细介绍了java DecimalFormat的常用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • 一文教你搞定Java Optional类判空操作

    一文教你搞定Java Optional类判空操作

    有时项目组内做code review,会充斥着大量的、原始的、丑陋的判空语句。让整体的代码显得十分的臃肿庞大丑陋,那么怎么办呢?利用Optional这个jdk8中引入的类就可以优雅的处理,现在我们来详细讲解下这个类的使用和源码
    2022-10-10
  • 详解HandlerInterceptor处理器拦截器的用法

    详解HandlerInterceptor处理器拦截器的用法

    这篇文章主要介绍了HandlerInterceptor处理器拦截器的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • java.lang.NoSuchMethodException: com.sun.proxy.$Proxy58.list错误解决办法

    java.lang.NoSuchMethodException: com.sun.proxy.$Proxy58.list

    这篇文章主要介绍了java.lang.NoSuchMethodException: com.sun.proxy.$Proxy58.list错误解决办法的相关资料,需要的朋友可以参考下
    2016-12-12
  • 在Java代码中解析html,获取其中的值方法

    在Java代码中解析html,获取其中的值方法

    今天小编就为大家分享一篇在Java代码中解析html,获取其中的值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • JAVA定义变量与输出详解

    JAVA定义变量与输出详解

    这篇文章主要介绍了JAVA定义变量与输出详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 浅谈maven 多环境打包发布的两种方式

    浅谈maven 多环境打包发布的两种方式

    这篇文章主要介绍了浅谈maven 多环境打包发布的两种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Java8新特性Optional常用方法

    Java8新特性Optional常用方法

    optional类是Java8新增加的一个对象容器,主要的功能有对象的创建、获取、判断、过滤,映射等,下面这篇文章主要给大家介绍了关于Java8新特性Optional常用方法的相关资料,需要的朋友可以参考下
    2024-02-02

最新评论