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());
}