package com.jmw.utils;
import java.io.File;
import java.util.Date;
import org.apache.log4j.Logger;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
public class Doc2PdfUtil extends Thread{
public static enum STATUS {
SUCCESS, FAIL, NOINSTALL
};
private static OfficeManager officeManager;
//private static String OFFICE_HOME = "C:\\Program Files\\OpenOffice.org 3";
private static String OFFICE_HOME = ConstantPath.OfficeHome;
//private static int port[] = {7605,7606,7607};
private static int port[] = {7605,7606};
public static STATUS convert2PDF(File inputFile, File pdfFile) {
Date start = new Date();
try{
System.setProperty("office.home", OFFICE_HOME);
startService();
Logger.getLogger(Doc2PdfUtil.class).info("进行文档转换:" + inputFile.getName() );
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(inputFile, pdfFile);
}catch (Exception e) {
System.out.println("convert2PDF Message = " + e.getMessage() + " Cause = " + e.getCause());
return STATUS.NOINSTALL;
}finally{
stopService();
}
long l = (start.getTime() - new Date().getTime());
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
System.out.println("convert2PDF doc 转 pdf 生成耗费:" + min + "分" + s + "秒");
Logger.getLogger(Doc2PdfUtil.class).info("生成" + pdfFile.getName() + "耗费:" + min + "分" + s + "秒");
if (pdfFile.exists()) {
return STATUS.SUCCESS;
} else {
return STATUS.FAIL;
}
}
public static void startService() {
System.out.println("调用Doc2PdfUtil startService....");
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
configuration.setOfficeHome(OFFICE_HOME);// 设置OpenOffice.org安装目录
configuration.setPortNumbers(port); // 设置转换端口,默认为8100
configuration.setMaxTasksPerProcess(2);
configuration.setTaskExecutionTimeout(1000 * 60 * 3L);// 设置任务执行超时3分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时24小时
System.out.println("officeManager start configuration.toString =" + configuration.toString());
officeManager = configuration.buildOfficeManager();
officeManager.start(); // 启动服务
}catch(IllegalStateException se){
System.out.println(" startService Message1 = " + se.getMessage() + " Cause2 = " + se.getCause() + " LocalizedMessage() = " + se.getLocalizedMessage());
se.printStackTrace();
} catch (Exception ce) {
System.out.println("office转换服务启动失败!详细信息:" + ce);
}
}
public static void stopService() {
System.out.println("关闭office转换服务....");
if (officeManager != null) {
officeManager.stop();
}
System.out.println("关闭office转换成功!");
}
}
------解决思路----------------------
没研究过这个呢
------解决思路----------------------
用的哪个版本的jodconverter?我没找到你的这个库。
是否两个线程同时写了一个文件?又没有特殊处理。这是不允许的。