前言
因为网上的信息比较多,每个项目又不太一样,故整理一下以作记录。
一、下载openoffice
下载地址:https://www.openoffice.org/download/
下载后,安装即可。
二、引入依赖
使用的JodConverter版本为2.2.1。因为jodconverter2.2.1必须依赖slf4j-jdk14必须这个版本,但是因为该版本过低,所以使用slf4j-api,slf4j-log4j12作为替代代替。
<!--openOffice所需依赖-->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
代码实现
private static String OPENOFFICE_START = "E:\\it\\OpenOffice4\\program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";//openoffice安装路径以及启动命令
private static String OPENOFFICE_IP = "127.0.0.1";//ip
private static int OPENOFFICE_PORT = 8100;//端口
private static String FILE_PATH = "D:\\fileConversion\\sourceFile";//源文件夹
private static String DESTFILE_PATH = "D:\\fileConversion\\destFile\\";//目标文件夹
private static String DESTFILE_SUFFIX = ".pdf";//文件后缀
//使用openoffice 将word格式的文件转换为pdf格式
@Test
public void OfficeTest(){
//获取源文件的文件夹下所有文件
Map fileMap = null;
Process progress = null;//openOffice进程
OpenOfficeConnection connection = null;//openOffice连接
// 调用openoffice服务线程
try {
if (progress == null){
progress = Runtime.getRuntime().exec(OPENOFFICE_START);
}
connection = new SocketOpenOfficeConnection(OPENOFFICE_IP, OPENOFFICE_PORT);//创建连接对象
connection.connect();
//用于测试openOffice连接时间
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println("连接时间:" + df.format(new Date()));
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
connection);//创建转换器
/**
* 获取文件夹下所有文件的集合
**/
fileMap = readfile(FILE_PATH);
Set<Map.Entry<String,String>> setFile = fileMap.entrySet();
for (Map.Entry<String, String> file : setFile) {
String fileName = file.getKey();//文件名
String filePath = file.getValue();//绝对路径
String destFileName = fileName.substring(0, fileName.lastIndexOf("."));//获取不带后缀的文件名
//转换文件
converter.convert(filePath, DESTFILE_PATH + destFileName + DESTFILE_SUFFIX);
}
System.out.println("转换完成");
} catch (IOException e) {
e.printStackTrace();
}finally {
if (connection != null) {
// 关闭连接
System.out.println("关闭连接");
connection.disconnect();
}
if (progress != null) {
// 关闭进程
System.out.println("关闭进程");
progress.destroy();
}
}
}
获取文件夹下所有文件
/**
* 读取某个文件夹下的所有文件(支持多级文件夹)
*/
public Map readfile(String filepath) throws FileNotFoundException, IOException {
//声明一个HashMap,用于存放源文件,格式:<文件名,绝对路径>,以文件名为KEY,可以得到整个文件所在的路径和文件名
Map<String, String> fileMap = new HashMap<>();
try {
File file = new File(filepath);
if (!file.isDirectory()) {
//文件
fileMap.put(file.getName(),file.getAbsolutePath());
} else if (file.isDirectory()) {
//文件夹
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
fileMap.put(readfile.getName(),readfile.getAbsolutePath());
} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return fileMap;
}
注意JodConverter2.2.1不兼容docx、xlsx、pptx格式。
解决方法:重写BasicDocumentFormatRegistry类。新建com.artofsolving.jodconverter包
/**
* openOffice工具类,用于解决不兼容docx\xlsx和pptx,导致转换pdf异常问题
*/
public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
private List documentFormats = new ArrayList();
public BasicDocumentFormatRegistry() {
}
public void addDocumentFormat(DocumentFormat documentFormat) {
this.documentFormats.add(documentFormat);
}
protected List getDocumentFormats() {
return this.documentFormats;
}
public DocumentFormat getFormatByFileExtension(String extension) {
if (extension == null) {
return null;
} else {
if (extension.indexOf("doc") >= 0) {
extension = "doc";
}
if (extension.indexOf("ppt") >= 0) {
extension = "ppt";
}
if (extension.indexOf("xls") >= 0) {
extension = "xls";
}
String lowerExtension = extension.toLowerCase();
Iterator it = this.documentFormats.iterator();
DocumentFormat format;
do {
if (!it.hasNext()) {
return null;
}
format = (DocumentFormat)it.next();
} while(!format.getFileExtension().equals(lowerExtension));
return format;
}
}
public DocumentFormat getFormatByMimeType(String mimeType) {
Iterator it = this.documentFormats.iterator();
DocumentFormat format;
do {
if (!it.hasNext()) {
return null;
}
format = (DocumentFormat)it.next();
} while(!format.getMimeType().equals(mimeType));
return format;
}
}
总结:结合网上信息,进行资源整合,作为笔记。