1.引入jar包,使用maven命令安装到本地maven仓库
链接:https://pan.baidu.com/s/1_sgMffKIgJkFRCHImt5_vA 提取码:j7mj
<!--openoffice使用所需要的jar包-->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.juh</groupId>
<artifactId>juh</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.jurt</groupId>
<artifactId>jurt</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.ridl</groupId>
<artifactId>ridl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.unoil</groupId>
<artifactId>unoil</artifactId>
<version>3.0.1</version>
</dependency>
2.下载openofficewindow版本和linux版本,也可以去官网下载最新版
链接:https://pan.baidu.com/s/1jQfKxyI0XCPx8T5KlxCJpA 提取码:yxda
链接:https://pan.baidu.com/s/1c5kUf7TNfG_RV3UhIrgvOQ 提取码:1ux0
window版本直接安装,选择默认目录即可。安装完成打开命令行,进入program目录
启动openoffice
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard
查看8100端口是否打开,打开说明启动成功
linux版本安装教程
tar -zxvf Apache_OpenOffice_4.1.10_Linux_x86-64_install-rpm_zh-CN.tar.gz
cd zh-CN/
cd RPMS/
rpm -ivh *.rpm
cd desktop-integration/
rpm -ivh openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm
启动:
/opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
查看端口是否打开
losf -i:8100
注意:linux环境下需要执行以下操作,否则无法识别中文
1. $JAVA_HOME/jre/lib/fonts/fallback文件夹,没有就新建
将字体:simhei.ttf 黑体、simsun.ttc 宋体(windows系统中有)
上传至$JAVA_HOME/jre/lib/fonts/fallback路径下。
2. 查看系统字体文件路径
cat /etc/fonts/fonts.conf
<!-- Font directory list -->
<dir>/usr/share/fonts</dir>
<dir>/usr/share/X11/fonts/Type1</dir> <dir>/usr/share/X11/fonts/TTF</dir> <dir>/usr/local/share/fonts</dir>
<dir>~/.fonts</dir>
3. $JAVA_HOME/jre/lib/fonts的全部内容复制到usr/share/fonts目录下
4.更新缓存 fc-cache
5.重启openoffice
1.使用代码连接openoffice进程
public static int office2PDF(File sourceFile, File destFile) {
try {
//连接openoffice服务
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(sourceFile, destFile);
connection.disconnect();
// 关闭进程
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
2.预览文件
@RequestMapping(value = {"/review"}, produces = {"application/json; charset=utf-8"})
public void reviewReport(String id,HttpServletResponse response) throws Exception {
log.info("文件预览开始id={}",id);
FileData fileData=fileDataService.selectByPrimaryKey(Integer.valueOf(id));//查看需要预览的文件路径
File sourceFile = new File(fileData.getFilePath()+"/"+fileData.getFileName());//获取文件
File destFile = File.createTempFile("temp", ".pdf");//转化成pdf格式
OfficeUtils.office2PDF(sourceFile, destFile);//源文件输入到pdf
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/pdf");
InputStream fis = new BufferedInputStream(new FileInputStream(destFile));//放入流
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
//response.reset();
OutputStream toClient = new BufferedOutputStream((OutputStream)response.getOutputStream());
toClient.write(buffer);写入到response中
toClient.flush();
toClient.close();
log.info("文件预览结束fileName={}",sourceFile.getName());
}