您的位置:首页 > 运维架构

web使用openoffice实现在线预览office文档

2015-08-24 16:16 302 查看
最近搞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();
// 连接OpenOffice
OfficeDocumentConverter 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: