点击关注公众号,实用技术文章及时了解

来源:blog.csdn.net/weixin_40986713/

article/details/109527294

java实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司专门提供这样的服务,不过需要收费 如果想要免费的,可以用openoffice,实现原理就是:

通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件流;

当然如果装了Adobe Reader XI,那把pdf直接拖到浏览器页面就可以直接打开预览,前提就是浏览器支持pdf文件浏览。

我这里介绍通过poi实现word、excel、ppt转pdf流,这样就可以在浏览器上实现预览了。

1.到官网下载Apache OpenOffice 安装包,安装运行。(不同系统的安装方法,自行百度,这里不做过多说明)

2.再项目的pom文件中引入依赖

<!--openoffice-->
<dependency><groupId>com.artofsolving</groupId><artifactId>jodconverter</artifactId><version>2.2.1</version>
</dependency>

3.将word、excel、ppt转换为pdf流的工具类代码

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;/*** 文件格式转换工具类** @author tarzan* @version 1.0* @since JDK1.8*/
public class FileConvertUtil {/** 默认转换后文件后缀 */private static final String DEFAULT_SUFFIX = "pdf";/** openoffice_port */private static final Integer OPENOFFICE_PORT = 8100;/*** 方法描述 office文档转换为PDF(处理本地文件)** @param sourcePath 源文件路径* @param suffix     源文件后缀* @return InputStream 转换后文件输入流* @author tarzan*/public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {File inputFile = new File(sourcePath);InputStream inputStream = new FileInputStream(inputFile);return covertCommonByStream(inputStream, suffix);}/*** 方法描述  office文档转换为PDF(处理网络文件)** @param netFileUrl 网络文件路径* @param suffix     文件后缀* @return InputStream 转换后文件输入流* @author tarzan*/public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception {// 创建URLURL url = new URL(netFileUrl);// 试图连接并取得返回状态码URLConnection urlconn = url.openConnection();urlconn.connect();HttpURLConnection httpconn = (HttpURLConnection) urlconn;int httpResult = httpconn.getResponseCode();if (httpResult == HttpURLConnection.HTTP_OK) {InputStream inputStream = urlconn.getInputStream();return covertCommonByStream(inputStream, suffix);}return null;}/*** 方法描述  将文件以流的形式转换** @param inputStream 源文件输入流* @param suffix      源文件后缀* @return InputStream 转换后文件输入流* @author tarzan*/public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception {ByteArrayOutputStream out = new ByteArrayOutputStream();OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_PORT);connection.connect();DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX);DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix);converter.convert(inputStream, sourceFormat, out, targetFormat);connection.disconnect();return outputStreamConvertInputStream(out);}/*** 方法描述 outputStream转inputStream** @author tarzan*/public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {ByteArrayOutputStream baos=(ByteArrayOutputStream) out;return new ByteArrayInputStream(baos.toByteArray());}public static void main(String[] args) throws IOException {//convertNetFile("http://172.16.10.21/files/home/upload/department/base/201912090541573c6abdf2394d4ae3b7049dcee456d4f7.doc", ".pdf");//convert("c:/Users/admin/Desktop/2.pdf", "c:/Users/admin/Desktop/3.pdf");}
}

4.serve层在线预览方法代码

/*** @Description:系统文件在线预览接口* @Author: tarzan*/public void onlinePreview(String url, HttpServletResponse response) throws Exception {//获取文件类型String[] str = SmartStringUtil.split(url,"\\.");if(str.length==0){throw new Exception("文件格式不正确");}String suffix = str[str.length-1];if(!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls")&& !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx")){throw new Exception("文件格式不支持预览");}InputStream in=FileConvertUtil.convertNetFile(url,suffix);OutputStream outputStream = response.getOutputStream();//创建存放文件内容的数组byte[] buff =new byte[1024];//所读取的内容使用n来接收int n;//当没有读取完时,继续读取,循环while((n=in.read(buff))!=-1){//将字节数组的数据全部写入到输出流中outputStream.write(buff,0,n);}//强制将缓存区的数据进行输出outputStream.flush();//关流outputStream.close();in.close();}

5.controler层代码

@ApiOperation(value = "系统文件在线预览接口 by tarzan")@PostMapping("/api/file/onlinePreview")public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception{fileService.onlinePreview(url,response);}
推荐:
主流Java进阶技术(学习资料分享)PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!

快速实现word、excel、ppt、txt等办公文件在线预览功能(Java版)相关推荐

  1. Java 实现word、excel、ppt、txt等办公文件在线预览功能!

    大家好,我是宝哥! 如何用 Java 实现word.excel.ppt.txt等办公文件在线预览功能?本文告诉你答案! java 实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司 ...

  2. 手把手教你用 Java 实现word、excel、ppt、txt等办公文件在线预览功能!

    如何用 Java 实现word.excel.ppt.txt等办公文件在线预览功能?本文告诉你答案! java 实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司专门提供这样的服务, ...

  3. word、excel、ppt 办公文件 在线预览

    如果想要免费的,可以用 openoffice,实现原理就是: 通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件流:当然如果装了Adobe Reader ...

  4. 将office 的文件,word,xlsx,ppt,txt 转成pdf 供预览

    java实现在线预览功能--openoffice(支持xlx,xlxs,txt,word,ppt等格式)_Bestest~的博客-CSDN博客_openoffice 能预览哪些格式功能说明:利用ope ...

  5. 文件在线预览 图片 PDF Excel Word

    1.前端实现pdf文件在线预览功能 方式一.pdf文件理论上可以在浏览器直接打开预览但是需要打开新页面.在仅仅是预览pdf文件且UI要求不高的情况下可以直接通过a标签href属性实现预览 <ah ...

  6. 计算机异常情况处理ppt,【计算机技能小课堂:Word/Excel/PPT电脑异常导致文件丢失,如何恢复?】- 环球网校...

    [摘要]大家周三好呀!各位环球网校的小伙伴们,新年快乐呀,2020大家要加油哦.今天这一期计算机技能小课堂:word技巧干货!Word Excel PPT电脑异常导致文件丢失,如何恢复.这个主题听起来 ...

  7. txt doc rtf html,JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例.docx

    JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例??2012-06-2 ...

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

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

  9. office 文档 在线预览功能实现(word,excel,pdf,ppt等多种格式)——使用https://view.xdocin.com/view 提示文档过期——基础积累

    web实现office文档在线预览功能--基础积累 最近遇到一个需求,就是要实现多种文档链接的在线预览,最简单的方式就是通过window.open(url地址)的方式来实现. 但是如果要求是在一个弹窗 ...

最新文章

  1. idea中文乱码问题
  2. 学习笔记Flink(六)—— Flink DataStream API编程
  3. 《Effective C++》item25:考虑写出一个不抛异常的swap函数
  4. C语言第五章实验二答案,《C语言程序设计》实验二 参考答案
  5. Linux下面的IO模型
  6. 企业员工工资管理系统
  7. 摩根大通:iPhone 13和新款iPhone SE将推动苹果业绩明年再破纪录
  8. MDT2010-windows 7镜像捕获与模板镜像部署(二)(特别版)
  9. NGINX(四)配置解析
  10. asp.net 母版页使用详解--转
  11. Go语言-测试与性能分析
  12. IDEA开发环境中maven 项目配置使用JDK9,JDK10,JDK11,JDK12等
  13. SD卡与TF卡基础知识
  14. 博客留言外链是否有用?
  15. 如何给win10桌面添加便签,win10桌面添加便签的方法
  16. U盘中毒文件乱码怎么办
  17. pytorch基础(九)- 自定义数据集训练模型 和 迁移学习
  18. 洛谷2591BZOJ2298 problem a题解
  19. JS XML在线格式化、压缩、校验、XML转JSON工具-toolfk程序员工具网
  20. lr中的lr_output_message,Lr_debug_message,Lr_error_message,Lrd_stmt,Lrd_fetch函数

热门文章

  1. 王思聪连收3条限制消费令后,债主回应:对于他是小钱,对于我们可是巨款
  2. 已然是身份的象征了?Coach品牌再入天猫 只有目标用户才有机会看到
  3. 左手手机右手智慧屏 华为9月要搞大事情
  4. 华为Mate 30系列发布日期、地点再曝光:9月19日 慕尼黑见?
  5. socket中使用心跳来检测连接是否断开[ZT]
  6. 2008最新热门搞笑的50条语录
  7. glassfish显示不了html文件,Glassfish websocket无法正常工作(示例代码)
  8. Flutter 踩坑 在bottomNavigationBar下显示bottomSheet
  9. 数据结构与算法概述——C语言
  10. axure原型案例_AXURE原型设计:移动端搜索原型案例