JavaWeb项⽬使⽤openoffice进⾏附件预览office⽂档
(doc,docx,。。。
最近搞web项⽬,使⽤框架SpringMVC+iBatis实现,做到项⽬⾥⾯⼀个附件在线预览功能,试过⽆数的⽅法,最后得到了⼀个⾮常使⽤的⽅法,这⽅法也是我看过多篇博客总结前⼈的⽂章出来的,仅限参考。
我们先来看看效果图
第⼀步:
通过第三⽅软件openoffice将office⽂档ppt,pptx,doc,docx,xls,xlsx转换成pdf⽂档;
第⼆步:
JODConverter⼀个Java的OpenDocument ⽂件转换器,导⼊其相关的jar包
第三步:
进⾏安装⽂件,在进⾏项⽬开发前,必须启动openoffice,我这⾥不需要之前启动openoffice,启动openoffice写在代码中,使⽤代码进⾏启动;不过你唯⼀要改的就是你的openoffice安装路径,这⾥在后边我会说到的。
上⾯相关jar包导⼊后,⽽且openoffice也安装好后,就可以进⾏项⽬开发:
⾸先给出⼯具类,很重要:
package com.askingdee.htglmon.utils;
import java.io.File;
import java.io.File;
import java.util.Date;
import 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>
*
* @date 2017-09-11
* @author zt
*
*/
public class Office2PDF {
/
**
* office中.doc格式
*/
public static final String OFFICE_DOC = "doc";
/**
* office中.docx格式
*/
public static final String OFFICE_DOCX = "docx";
/**
* office中.xls格式
*/
public static final String OFFICE_XLS = "xls";
/**
* office中.xlsx格式
*/
public static final String OFFICE_XLSX = "xlsx";
/**
* office中.ppt格式
*/
public static final String OFFICE_PPT = "ppt";
/**
* office中.pptx格式
*/
public static final String OFFICE_PPTX = "pptx";
/**
* pdf格式
*/
public static final String OFFICE_TO_PDF = "pdf";
public static void main(String[] args) {
Office2PDF office2pdf = new Office2PDF();
office2pdf.openOfficeToPDF("e:/test." + OFFICE_DOCX, "e:/test_" + new Date().getTime() + "." + OFFICE_TO_PDF);  office2pdf.openOfficeToPDF("e:/test." + OFFICE_PPTX, null);
}
/**
* 使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);
}
/
**
* 根据操作系统的名称,获取 3的安装⽬录<br>
* 如我的 3安装在:C:/Program Files (x86)/ 3<br>
*
* @ 3的安装⽬录
*/
public String getOfficeHome() {
String osName = Property("os.name");
if (Pattern.matches("Linux.*", osName)) {
return "/4";
} else if (Pattern.matches("Windows.*", osName)) {
return "C:/Program Files (x86)/OpenOffice 4";
} else if (Pattern.matches("Mac.*", osName)) {
return "/app/Contents";
}
return null;
}
/**
* 连接 并且启动
*
* @return
*/
public OfficeManager getOfficeManager() {
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
// 获取 3的安装⽬录
String officeHome = getOfficeHome();
config.setOfficeHome(officeHome);
// 启动OpenOffice的服务
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
mkdirs方法
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 (!ParentFile().exists()) {
}
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();
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 (ists()) {// 不到源⽂件, 则返回
converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);    flag = true;
}
} else {
if (ists()) {// 不到源⽂件, 则返回
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 = placeAll("." + 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);
}
}
配置servert的xml
<!-- 预览附件Servlet -->
<servlet>
<servlet-name>YulanServlet</servlet-name>
<servlet-class>com.askingdee.htglmon.servlet.YulanServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>YulanServlet</servlet-name>
<url-pattern>/a/servlet/YulanServlet/*</url-pattern>
</servlet-mapping>
YulanServlet代码

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。