web使用openoffice实现在线预览office文档
最近搞web项目,使用框架struts+spring+jpa实现,做到项目里面一个在线预览功能,试过无数的方法,最后得到了一个非常使用的方法,这方法也是我看过多篇博客的出来的,仅限参考。
效果图如下:
第一步:
通过第三方软件openoffice将office文档ppt,pptx,doc,docx,xls,xlsx转换成pdf文档;
openoffice下载链接:http://www.openoffice.org/zh-cn/download/,
第二步:
JODConverter一个Java的OpenDocument 文件转换器,导入其相关的jar包
下载地址:http://download.csdn.net/detail/tan313/9041821
第三步:
进行安装文件,在进行项目开发前,必须启动openoffice,我这里不需要之前启动openoffice,启动openoffice写在代码中,使用代码进行启动;不过你唯一要改的就是你的openoffice安装路径,这里在后边我会说到的。
上面相关jar包导入后,而且openoffice也安装好后,就可以进行项目开发:
首先给出工具类,很重要:
/*** */
package com.sdbd.utils;import java.io.File;
import java.util.Date;
import java.util.regex.Pattern;import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;/*** 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx)* 转化为pdf文件<br>* Office2010的没测试<br>* * @date 2012-11-5* @author xhw* */
public class Office2PDF {/*** 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>* * @param inputFilePath* 源文件路径,如:"e:/test.docx"* @param outputFilePath* 目标文件路径,如:"e:/test_docx.pdf"* @return*/public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {return office2pdf(inputFilePath, outputFilePath);}/*** 根据操作系统的名称,获取OpenOffice.org 3的安装目录<br>* 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3<br>* * @return OpenOffice.org 3的安装目录*/public String getOfficeHome() {String osName = System.getProperty("os.name");System.out.println("操作系统名称:"+osName);if (Pattern.matches("Linux.*", osName)) {return "/opt/openoffice.org3";} else if (Pattern.matches("Windows.*", osName)) {return "C:/Program Files/OpenOffice 4";} else if (Pattern.matches("Mac.*", osName)) {return "/Application/OpenOffice.org.app/Contents";}return null;}/*** 连接OpenOffice.org 并且启动OpenOffice.org* * @return*/public OfficeManager getOfficeManager() {DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();// 获取OpenOffice.org 3的安装目录String officeHome = getOfficeHome();config.setOfficeHome(officeHome);// 启动OpenOffice的服务OfficeManager officeManager = config.buildOfficeManager();officeManager.start();return officeManager;}/*** 转换文件* * @param inputFile* @param outputFilePath_end* @param inputFilePath* @param outputFilePath* @param converter*/public void converterFile(File inputFile, String outputFilePath_end, String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) {File outputFile = new File(outputFilePath_end);// 假如目标路径不存在,则新建该路径if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}converter.convert(inputFile, outputFile);System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!");}/*** 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>* * @param inputFilePath* 源文件路径,如:"e:/test.docx"* @param outputFilePath* 目标文件路径,如:"e:/test_docx.pdf"* @return*/public boolean office2pdf(String inputFilePath, String outputFilePath) {boolean flag = false;OfficeManager officeManager = getOfficeManager();// 连接OpenOfficeOfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);long begin_time = new Date().getTime();if (null != inputFilePath) {File inputFile = new File(inputFilePath);// 判断目标文件路径是否为空if (null == outputFilePath) {// 转换后的文件路径String outputFilePath_end = getOutputFilePath(inputFilePath);if (inputFile.exists()) {// 找不到源文件, 则返回converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);flag = true;}} else {if (inputFile.exists()) {// 找不到源文件, 则返回converterFile(inputFile, outputFilePath, inputFilePath, outputFilePath, converter);flag = true;}}officeManager.stop();} else {System.out.println("con't find the resource");}long end_time = new Date().getTime();System.out.println("文件转换耗时:[" + (end_time - begin_time) + "]ms");return flag;}/*** 获取输出文件* * @param inputFilePath* @return*/public String getOutputFilePath(String inputFilePath) {String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");return outputFilePath;}/*** 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br>* * @param inputFilePath* @return*/public String getPostfix(String inputFilePath) {return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);}}
在该工具类中,你需要改动的就是getOfficeHome()函数中返回值为你的openoffice的安装路径。
工具类好了,在web中当我们点击预览按钮提交到一个action或者servlet处理
在处理类中唯一需要的参数是你预览原文件的的路径,记住,是绝对路径
根据相对路径获取绝对路径方法:
String realpathdir = request.getSession().getServletContext().getRealPath(pathdir);
获取到绝对路径就可以调用工具类进行转换:
office2pdf.openOfficeToPDF(filePath, 你需要存储的路径+"/" + 文件名 +".pdf");
filePath为原文件的绝对路径,第二个参数为你需要存储的路径和文件名,在这里我处理的方法写一个相对路径,然后获取其绝对路径,后边就跟上你转换的文件名了。
转换好后,就开始进行预览,
部门代码如下:
file = new File(convertrealpath);//convertrealpath为你转换好后的文件的绝对路径
URL u = new URL("file:///" + convertpath); BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8")); OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
return null;
以上就可以做到在线预览。经过检查,office系列文档ppt,pptx,xls,xlsx,doc,docx都能够预览。
项目源码不能够展示,给个文档转换源码:(转换后其实就好做了,把上面那个代码拷贝,路径放进去就能够实现预览):
http://download.csdn.net/detail/tan313/9041835
参阅博客:http://blog.csdn.net/z69183787/article/details/17468039
web使用openoffice实现在线预览office文档相关推荐
- java预览openoffice_web使用openoffice实现在线预览office文档
最近搞web项目,使用框架struts+spring+jpa实现,做到项目里面一个在线预览功能,试过无数的方法,最后得到了一个非常使用的方法,这方法也是我看过多篇博客的出来的,仅限参考. 效果图如下: ...
- web项目使用OpenOffice实现前端在线预览office文档(超详细)
超详细的OpenOffice实现前端在线预览office文档记录 最近搞一个数字化共享平台,是一个java web项目,使用框架ssm,其中项目有一个需要在线预览PDF.excle.ppt.word文 ...
- 安卓WebView在线预览office文档功能实现
*## 在线预览office文档 我就直奔主题吧!估计是目前最简单的了吧! 我也搜了很多资料不外乎2种 使用谷歌文档服务(google国内使用不了,各种试就是显示不出来,) mWebView.load ...
- Android实现在线预览office文档(Word,Pdf,excel,PPT.txt等格式)
1.概述 我们都知道,Android原生并没有提供浏览office文档格式的相关Api,在安卓端想要实现在线预览office文档的功能显然很是复杂,我们手机安装QQ浏览器时,在手机打开office文档 ...
- 调用office web 365接口实现在线预览word文档,PDF,PPT
我项目中是直接用iframe显示: <iframe id="iframe_src" scrolling="auto" width="100% ...
- Office文档上传后实时转换为PDF格式_图片文件上传后实时裁剪_实现在线预览Office文档
Office文档上传后实时转换为PDF格式_图片文件上传后实时裁剪 前置条件 安装LibreOffice 安装OpenOffice 安装Unoconv 安装ImageMagick.x86_64 安装G ...
- 利用微软的Office Online在线预览Office文档
一个url就可以访问了: https://view.officeapps.live.com/op/view.aspx?src=文件地址 这个文件地址需满足以下几个条件: (1)在浏览器是可以访问的: ...
- web项目 在线预览doc文档
在线预览word文档 前端代码 后台controller代码(使用Aspose.Words) 前端代码 在前端发送ajax请求,并展示后端生成的html临时文件 showFile:function(m ...
- 在线预览word文档
在线预览word文档 在线预览word文档 Java 使用wps将word文件转换pdf文件 转成PDF 在线预览word(转成pdf)前端展示 欢迎来提更好的意见 在线预览word文档 昨天下午组长 ...
最新文章
- declare handler 声明异常处理的语法
- Oracle-使用切片删除的方式清理非分区表中的超巨数据
- get中添加header
- 软件测试测试用例编写_不要先编写所有软件测试-只需编写一个
- 生物效应大数据评估聚类算法的并行优化
- Magento 页面中加入CMS static block
- Windows10 Kafka Docker 集群搭建
- java抓取屏幕像素,以像素为单位获取屏幕尺寸
- 雨过天晴电脑保护系统 试用手记
- 如何把两个文件合并成pdf文件?
- CNCERT发布《2018年我国互联网网络安全态势报告》
- 《JAVASE系列》抽象类与接口
- Pycharm以及cmd调用Anaconda已配置环境的方法
- 白话中台战略2:中台到底长啥样?
- CCIE认证通过率一般是多少?
- Android系统编译优化:使用Ninja加快编译
- 【日本IT】2018日本开发语言收入排名大公开 | 快来看看你的Java、.Net上榜了吗?
- 网站建设基本流程规范
- 移动CRM风起云涌 千亿级市场显现
- Markdown(5):锚链接