文章目录
摘要,本文是使用了open office将doc、docx等文件格式转化为txt、pdf或html,效率与使用的操作系统相关,采用Linux效率普遍高于windows,所以在使用时,别单一注重了服务器性能,而忘记了操作系统对性能的影响。
1、安装openoffice(windows版忽略)
先到http://www.openoffice.org/zh-cn/download/下载需要的版本,用ftp传输到服务器或直接用wget方式下载到服务器。
- tar xvf Apache_OpenOffice_4.1.3_Linux_x86-64_install-rpm_zh-CN.tar.gz
- cd zh-CN/RPMS/
- rpm -ivh *
- rpm -ivh *.rpm
2、上传Windows字体到linux(解决乱码问题)
- 找到windows下的所有字体(simhei.ttf 黑体、simsun.ttc 宋体等)
- echo $JAVA_HOME #查看jdk路径
- 把所有字体上传至”jdk安装路径”/jre/lib/fonts/fallback下
- cat /etc/fonts/fonts.conf #查看Linux系统字体安装路径
- 把所有字体上传至/usr/share/fonts下、
- fc-cache #跟新缓存
- 重启openoffice进程即可
3、java使用
Java使用openoffice主要是调用了 jodconverter-cli-2.2.2.jar包。
- pom文件引用了如下以来
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jodconverter-2.2.2.jar</systemPath>
</dependency>
- 代码核心
// 获取连接
OpenOfficeConnection connection = new SocketOpenOfficeConnection("C:\Program Files (x86)\OpenOffice 4");
try {
// 建立连接
connection.connect();
// 开始转换
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(file, pdfFile);
// 关闭连接
connection.disconnect();
} catch (ConnectException e) {
logger.error("转换失败,OpenOffice 服务未启动!", e);
throw new TSharkException(" 转换失败,OpenOffice 服务未启动!", e);
}
4、遇到坑总结
- InputStrem和OutputStrem为二进制流
- Java 与php 进行base64加密实现结果有不同,需要一方主动去兼容