简介

  之前有写了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构建输入文件

  */无锡做人流好的医院 http://www.120wtrlyy.com/

  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包。

Java实现在线预览--openOffice实现的更多相关文章

  1. Java实现在线预览–openOffice实现

    实现逻辑有两种: 一.利用jodconverter(基于OpenOffice服务)将文件(.doc..docx..xls..ppt)转化为html格式. 二.利用jodconverter(基于Open ...

  2. java实现在线预览--poi实现word、excel、ppt转html

    java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...

  3. java实现在线预览 - -之poi实现word、excel、ppt转html

    简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服务就可以了,例如永中office.office web 365(http://w ...

  4. Java实现在线预览功能

    java实现在线预览功能,需要用到  jacob.dll jacob.jar   预览pdf所需js  pdfobject.min.js 将上传文件转为pdf保存. <div class=&qu ...

  5. java实现附件预览(openoffice+swfTools+FlexPaper) (转载)

    下边例子是在网上找了一个网友做的例子,在次记录 1.概述 主要原理 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 2.通过swfTools将pd ...

  6. Java实现在线预览Word,Excel,Ppt文档

    效果图:

  7. 【Java】web实现图片在线预览

    一.场景还原 用户上传了一张图片,已有服务器保存路径,现由于系统配置无法直接通过图片URL打开预览图片,需实现点击预览将图片显示在浏览器上. 二.实现方法 html: <a href=" ...

  8. java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)

    背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...

  9. Java通过openOffice实现word,excel,ppt转成pdf实现在线预览

    Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...

随机推荐

  1. WebGL学习笔记(九):阴影

    3D中实现实时阴影技术中比较常见的方式是阴影映射(Shadow Mapping),我们这里也以这种技术来实现实时阴影. 阴影映射背后的思路非常简单:我们先以光的位置为视角进行渲染,我们能看到的东西都将 ...

  2. 【GMT43智能液晶模块】例程二十二:USB_CDC实验——高速数据传输

    源代码下载链接: 链接:https://pan.baidu.com/s/10KOWONWbNYlonyuX0W0Mcg 提取码:ggpo 复制这段内容后打开百度网盘手机App,操作更方便哦 GMT43 ...

  3. HDFS API 测试用例

    增加依赖 <!--hadoop--> <dependency> <groupId>org.apache.hadoop</groupId> <art ...

  4. 全基因组关联分析学习资料(GWAS tutorial)

    前言 很多人问我有没有关于全基因组关联分析(GWAS)原理的书籍或者文章推荐. 其实我个人觉得,做这个分析,先从跑流程开始,再去看原理. 为什么这么说呢,因为对于初学者来说,跑流程就像一个大黑洞,学习 ...

  5. beetl模版for循环渲染字符串

    beetl for循环渲染html字符串的方式, beetl if条件判断输出, beet自定义标签和标签引用, beetl html赋值, beetl渲染json,beetl注释.变量定义, 更多文 ...

  6. 图片转化base64格式

    public function Base64EncodeImage($ImageFile) { // 图片转化base64格式 , 图片需要在本地,有访问权限 , 相对于项目路径 if(file_ex ...

  7. 项目在服务器部署后打开出现Invalid Host header

    一.问题描述在服务器部署启动了项目,页面显示Invalid Host header. 二.问题分析新版的webpack-dev-server出于安全考虑,默认检查hostname,如果hostname ...

  8. 【论文阅读】PBA-Population Based Augmentation:Efficient Learning of Augmentation Policy Schedules

    参考 1. PBA_paper; 2. github; 3. Berkeley_blog; 4. pabbeel_berkeley_EECS_homepage; 完

  9. 分割nginx日志

    #!/bin/bash #此脚本用于自动分割Nginx的日志,包括access.log和error.log #每天00:00执行此脚本 将前一天的access.log重命名为access-xxxx-x ...

  10. [ARM-Linux开发] 主设备号--驱动模块与设备节点联系的纽带

    一.如何对设备操作 linux中对设备进行操作是通过文件的方式进行的,包括open.read.write.对于设备文件,一般称其为设备节点,节点有一个属性是设备号(主设备号.次设备号),其中主设备号将 ...