广告
返回顶部
首页 > 资讯 > 精选 >Java实现在线预览的示例代码(openOffice实现)
  • 251
分享到

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

javaopenofficeava 2023-05-30 20:05:47 251人浏览 泡泡鱼
摘要

简介之前有写了poi实现在线预览的文章,里面也说到了使用openOffice也可以做到,这里就详细介绍一下。 我的实现逻辑有两种: 一、利用jodconverter(基于OpenOffice服务)将文件(.doc、.docx、.xls、.p

简介

之前有写了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;public class Doc2HtmlUtil {  private static Doc2HtmlUtil doc2HtmlUtil;    public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {    if (doc2HtmlUtil == null) {      doc2HtmlUtil = new Doc2HtmlUtil();    }    return doc2HtmlUtil;  }    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();        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 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();        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");  }}

--结束END--

本文标题: Java实现在线预览的示例代码(openOffice实现)

本文链接: https://www.lsjlt.com/news/220784.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java实现在线预览的示例代码(openOffice实现)
    简介之前有写了poi实现在线预览的文章,里面也说到了使用openOffice也可以做到,这里就详细介绍一下。 我的实现逻辑有两种: 一、利用jodconverter(基于OpenOffice服务)将文件(.doc、.docx、.xls、.p...
    99+
    2023-05-30
    java openoffice ava
  • vue实现在线预览office文件的示例代码
    最近在做电子档案,后端提供了文件的华为云的oss链接。已经实现了点击下载文件的功能。但是呢,他们又希望常规的文件,可以直接点击预览,不需要下载。 按道理说,做文件的在线预览,买个第三...
    99+
    2022-11-12
  • Java实现线程插队的示例代码
    目录多线程5(线程插队)1.题目2.解题思路3.代码详解多线程5(线程插队) 1.题目 在编写多线程的业务时,会遇到让一个线程优先于其他线程运行的情况,除了可以设置线程的优先级高于其...
    99+
    2022-11-13
  • Vue实现预览文件(Word/Excel/PDF)功能的示例代码
    目录安装 引入组件、注册 使用之前也有写过两篇预览pdf的,但好像还没写过预览word和excel的,但是这次的预览pdf和之前的三个又不一样!使用pdfobje...
    99+
    2023-03-20
    Vue预览文件 Vue预览Word Vue预览Excel Vue预览PDF
  • Matlab实现灰色预测的示例代码
    目录模型介绍基础代码基础代码+修饰模型介绍 略微带过一下原理: 灰色预测对于趋势不强的数据,将其原始数据进行累加后得到具有明显趋势的新数据进行拟合,假设原数据为:  则新...
    99+
    2022-11-13
  • Office在线预览及PDF在线预览的实现方式大集合
    一、服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览 微软方:利用Office2007以上版本的一个PDF插件SaveAsPDFandXPS.exe可以导出PDF文件,然后再利用免费的swftools.exe工具生成sw...
    99+
    2023-08-31
    php word excel pdf
  • Java多线程Callable接口实现代码示例
    对于多线程,大家并不陌生,对于如何创建线程也是轻车熟路,对于使用new thread和实现runable接口的方式,不再多说。这篇博文我们介绍第三种:实现Callable接口。Callable接口接口定义:@FunctionalInterf...
    99+
    2023-05-30
    java 多线程 接口
  • Java实现办公文档在线预览功能
    java实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司专门提供这样的服务,不过需要收费 如果想要免费的,可以用openoffice,实现原理就是: 通过第三方...
    99+
    2022-11-12
  • Java实现Treap树的示例代码
    目录Treap树数据结构遍历查询增加删除完整代码Treap树 Treap树是平衡二叉搜索树的一种实现方式,但它不是完全平衡的。平衡二叉搜索树的实现方式还有AVL树、红黑树、替罪羊树、...
    99+
    2022-11-13
  • Java实现手写一个线程池的示例代码
    目录概述线程池框架设计代码实现阻塞队列的实现线程池消费端实现获取任务超时设计拒绝策略设计概述 线程池技术想必大家都不陌生把,相信在平时的工作中没有少用,而且这也是面试频率非常高的一个...
    99+
    2022-11-13
    Java手写线程池 Java线程池
  • Java实现PDF在线预览功能(四种方式)
    目录Java实现PDF在线预览Java快捷实现PDF在线预览Java实现PDF在线预览 @RequestMapping("/preview1") public v...
    99+
    2022-11-12
  • python实现线性回归的示例代码
    目录1线性回归1.1简单线性回归1.2 多元线性回归1.3 使用sklearn中的线性回归模型1线性回归 1.1简单线性回归 在简单线性回归中,通过调整a和b的参数值,来拟合从x到...
    99+
    2022-11-13
  • Monaco Editor实现sql和java代码提示实现示例
    目录monaco editor创建sql提示(库表字段关联)java自定义联想monaco editor创建 //创建和设置值 if (!this.monacoEditor) { ...
    99+
    2022-11-13
    Monaco Editor代码提示 sql java代码提示
  • Java实现手写乞丐版线程池的示例代码
    目录前言线程池的具体实现线程池实现思路线程池实现代码线程池测试代码杂谈总结前言 在上篇文章线程池的前世今生当中我们介绍了实现线程池的原理,在这篇文章当中我们主要介绍实现一个非常简易版...
    99+
    2022-11-13
    Java手写线程池 Java线程池
  • Java代码实现循环队列的示例代码
    循环队列结构 队列特点 队列为一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受...
    99+
    2022-11-12
  • Java实现Kruskal算法的示例代码
    目录介绍一、构建后的图二、代码三、测试介绍 构造最小生成树还有一种算法,即 Kruskal 算法:设图 G=(V,E)是无向连通带权图,V={1,2,...n};设最小生成树 T=(...
    99+
    2022-11-13
  • Java实现双链表的示例代码
    目录一、双向链表是什么二、具体方法实现定义结点下标访问异常获取链表长度打印链表清空链表头插法尾插法指定位置插入查找元素删除第一次出现的关键字删除所有值为key的节点三、完整代码一、双...
    99+
    2022-11-13
  • JAVA实现DOC转PDF的示例代码
    目录一、下载依赖二、代码实现三、转换结果四、后续研究五、总结Word作为目前主流的文本编辑软件之一,功能十分强大,应用人群广,但是它也存在一些问题。像是Word文件在不同软件或操作平...
    99+
    2022-11-12
  • Java实现Redis哨兵的示例代码
    前言: 本文将采用文字+代码的方式,讲解redis版哨兵的实现,所有代码都将写在一个类中,每个属性和方法都会结合文字加以说明。 1. 哨兵(Sentinel)主要功能如下: 1、不时...
    99+
    2022-11-13
  • Java实现Floyd算法的示例代码
    目录一 问题描述二 代码三 实现一 问题描述 求节点0到节点2的最短路径。 二 代码 package graph.floyd; ...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作