日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot 使用LibreOffice 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件

發布時間:2024/9/27 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot 使用LibreOffice 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

接上一篇:linux環境源碼安裝unoconv
Linux環境_源碼安裝Unoconv實現文件在線預覽doc,doxc,xls,xlsx,ppt,pptx 文件
https://gblfy.blog.csdn.net/article/details/103540694

接上一篇:linux環境yum安裝unoconv
unoconv 在線預覽 doc,doxc,xls,xlsx,ppt,pptx 文件功能環境搭建
https://gblfy.blog.csdn.net/article/details/102847276

實現原理:在Linux服務器上安裝unoconv 插件,插件作用是進行doc,doxc,xls,xlsx,ppt,pptx 五種類型文件的格式轉換。
最終都會轉換成以后綴名.pdf結尾的pdf文件,進行在線預覽。

文章目錄

  • 一、環境準備:
  • 二、需求案例
  • 三、實現思路:
  • 四、Linux預覽環境
  • 五、創建SpringBoot項目
  • 六、項目配置詳細
    • 6.1. pom依賴:
    • 6.2. 新建controller
    • 6.3. 在線預覽工具類
  • 七、linux環境測試
    • 7.1. 放置文件
    • 7.2. 運行項目
    • 7.3. 查看控制臺日志
    • 7.4. 瀏覽器訪問驗證
    • 7.5. 效果圖
  • 八、項目源碼

一、環境準備:

軟件版本
框架SpringBoot 2.1.1.RELEASE
unoconv0.6
LibreOffice6.3.2

二、需求案例

實現 doc,doxc,xls,xlsx,ppt,pptx 文件在線預覽
已知條件(需求文檔給出):

序號說明
文件存儲路徑/app/
文件名及文件格式 20191009133209lis_chgrpt.docx
訪問http://192.168.6.56/viewPDF 實現在線預覽

注:其他格式同上所述

三、實現思路:

序號說明
使用mvn打包項目 例:jar包
把 20191009133209lis_chgrpt.docx文件放到linux拂去其的/app/目錄下面
啟動項目:java -jar jarName
查看控制臺日志
瀏覽器訪問http://192.168.6.56/viewPDF 驗證在線預覽效果

四、Linux預覽環境

服務器是Linux環境,需要在Linux服務器安裝一個運行環境。詳細請參考文檔步驟進行安裝:
unoconv 在線預覽 doc,doxc,xls,xlsx,ppt,pptx 文件功能環境搭建
https://blog.csdn.net/weixin_40816738/article/details/102847276

五、創建SpringBoot項目




SpringBoot 項目創建完成!!!

六、項目配置詳細

6.1. pom依賴:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gblfy</groupId><artifactId>file-online-preview</artifactId><version>0.0.1-SNAPSHOT</version><name>file-online-preview</name><url>想學習更多知識請訪問 https://gblfy.com</url><description>SpringBoot在線預覽</description><properties><!--編碼設置--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!--JDK版本--><java.version>1.8</java.version></properties><dependencies><!--Springmvc啟動器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--excel導入導出--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.0.0</version></dependency><!--單元測試--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

6.2. 新建controller

package com.gblfy.file.onlinepreview.controller;import com.gblfy.file.onlinepreview.utils.LinuxPageDIsplsyFileUtil; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;/*** @author gblfy* @ClassNme FileController* @Description 文件在在線預覽* @Date 2019/11/1 8:09* @version1.0*/ @RestController public class FileOnlinePreviewController {/*** 在線預覽測試方法* 企業真實需求:* 文件的路徑 文件名 都需要動態獲取** @param response http響應網頁來實現在線預覽* @throws Exception*/@RequestMapping("/viewPDF")public void reviewWord(HttpServletResponse response)throws Exception{LinuxPageDIsplsyFileUtil linuxPageDIsplsyFileUtil = new LinuxPageDIsplsyFileUtil();//文件存儲路徑String fileStoragePath ="/app/";//轉換前的文件名String beforeConversion ="20191009133209lis_chgrpt.docx";/*** 文件格式轉換+在線預覽*/linuxPageDIsplsyFileUtil.conversionFile(response,fileStoragePath,beforeConversion);}@RequestMapping("/viewPDF2")public void reviewWord2(HttpServletResponse response)throws Exception{LinuxPageDIsplsyFileUtil linuxPageDIsplsyFileUtil = new LinuxPageDIsplsyFileUtil();//文件存儲路徑String fileStoragePath ="/app/";//轉換前的文件名String beforeConversion ="2019101114041220190929142601新人業務知識培訓2017.pdf";/*** 文件格式轉換+在線預覽*/linuxPageDIsplsyFileUtil.conversionFile(response,fileStoragePath,beforeConversion);} }

6.3. 在線預覽工具類

package com.gblfy.file.onlinepreview.utils;import org.apache.poi.util.IOUtils;import javax.servlet.http.HttpServletResponse; import java.io.*;/*** @Author : gblfy* @Date : 2019/11/01 11:20* @describe: 文檔在線預覽** 服務器環境:Linux環境* 現支持文檔類型: Excel word ppt pdf*/ public class LinuxPageDIsplsyFileUtil {private static LinuxPageDIsplsyFileUtil linuxPageDIsplsyFileUtil;public static synchronized LinuxPageDIsplsyFileUtil getSwitchUtil() {if (linuxPageDIsplsyFileUtil == null) {linuxPageDIsplsyFileUtil = new LinuxPageDIsplsyFileUtil();}return linuxPageDIsplsyFileUtil;}/*** 文檔在線預覽** @param response* @param fileStoragePath 文件存儲路徑 (前段獲取文件存儲路徑返給后臺)* @param beforeConversion 文件名(必須帶文件后綴名,這里指的就是文件全名稱)* @throws Exception*/public void conversionFile(HttpServletResponse response, String fileStoragePath, String beforeConversion) throws Exception {//文件存儲路徑//fileStoragePath ="/app/";//轉換前的文件名//beforeConversion ="20191009133209lis_chgrpt.docx";String fileNamePath = fileStoragePath + beforeConversion;File file = new File(fileNamePath);if (!file.exists()) {System.err.println("庫存中沒有指定文件。。。。");return;}//獲取到文件名String interceptFileName = beforeConversion.substring(0, beforeConversion.lastIndexOf("."));//截取文件后綴名String fileNameSuffix = beforeConversion.substring(beforeConversion.lastIndexOf(".") + 1);String command = null;if("pdf".equals(fileNameSuffix)){/*** 在線預覽方法*/openPdf(response, fileStoragePath + interceptFileName + ".pdf");}else if("doc".equals(fileNameSuffix)||"docx".equals(fileNameSuffix)||"xls".equals(fileNameSuffix)||"xlsx".equals(fileNameSuffix)||"ppt".equals(fileNameSuffix)||"pptx".equals(fileNameSuffix)) {//文件格式轉換命令 unoconv插件實現command = "/opt/libreoffice6.3/program/soffice --headless --invisible --convert-to pdf " + fileNamePath;//格式轉換+在線預覽formatConverAndPreview(command,response,fileStoragePath,interceptFileName);// }else if("docx".equals(fileNameSuffix)) {// command = "/usr/bin/unoconv -f pdf " + fileNamePath;// formatConverAndPreview(command,response,fileStoragePath,interceptFileName);// }else if("xls".equals(fileNameSuffix)) {// command = "/usr/bin/unoconv -f pdf " + fileNamePath;// formatConverAndPreview(command,response,fileStoragePath,interceptFileName);// }else if("xlsx".equals(fileNameSuffix)) {// command = "/usr/bin/unoconv -f pdf " + fileNamePath;// formatConverAndPreview(command,response,fileStoragePath,interceptFileName);// }else if("ppt".equals(fileNameSuffix)) {// command = "/usr/bin/unoconv -f pdf " + fileNamePath;// formatConverAndPreview(command,response,fileStoragePath,interceptFileName);// }else if("pptx".equals(fileNameSuffix)) {// command = "/usr/bin/unoconv -f pdf " + fileNamePath;// formatConverAndPreview(command,response,fileStoragePath,interceptFileName);}else{System.err.println("暫不支持該類型文件在線預覽!!!");return;}}/*** 格式轉換+在線預覽 方法** @param command 文件格式轉換命令 例:/usr/bin/unoconv -f pdf /app/1.pptx* @param response http響應網頁,實現在線預覽* @param fileStoragePath 準備文件存放路徑 例:/app/* @param interceptFileName 文件名 例: 1.pptx* @throws Exception*/public void formatConverAndPreview(String command,HttpServletResponse response,String fileStoragePath,String interceptFileName)throws Exception{/*** 格式轉換方法*///String temp ="/usr/bin/unoconv -f pdf " + command;executeCommand(command);/*** 在線預覽方法*/openPdf(response, fileStoragePath + interceptFileName + ".pdf");}/*** 在線預覽方法* 把轉換后的pdf文件在網頁上進行預覽** @param response http響應* @param previewFile 文件的決定路徑 例:/app/20191009133209_chgrpt.pdf* @throws Exception 格式轉換過程中的異常*/private static void openPdf(HttpServletResponse response, String previewFile) throws Exception {InputStream inputStream = null;OutputStream outputStream = null;//String path ="/app/20191009133209_chgrpt.pdf";inputStream = new FileInputStream(previewFile);//響應文件的類型response.setContentType("application/pdf");outputStream = response.getOutputStream();int a = 0;byte[] b = new byte[1024];while ((a = inputStream.read(b)) != -1) {outputStream.write(b, 0, a);}if (outputStream != null) {outputStream.close();}if (inputStream != null) {inputStream.close();}}/*** 格式轉換方法* <p>* 統一把文件轉換成pdf文件** @param command 文件格式轉換命令 例:/usr/bin/unoconv -f pdf /app/1.pptx* @throws Exception 格式轉換過程中的異常*/private static void executeCommand(String command) throws Exception {StringBuffer output = new StringBuffer();Process process;InputStreamReader inputStreamReader = null;BufferedReader reader = null;try {process = Runtime.getRuntime().exec(command);process.waitFor();inputStreamReader = new InputStreamReader(process.getInputStream(), "UTF-8");reader = new BufferedReader(inputStreamReader);String line = "";while ((line = reader.readLine()) != null) {output.append(line + "\n");}//p.destroy();//這個一般不需要} catch (Exception e) {e.printStackTrace();} finally {IOUtils.closeQuietly(reader);IOUtils.closeQuietly(inputStreamReader);}} }

七、linux環境測試

7.1. 放置文件

7.2. 運行項目

nohup java -jar jarName >msg.log 2>&1 &

7.3. 查看控制臺日志

tail -f msg.log

7.4. 瀏覽器訪問驗證

#在線預覽docx http://192.xxx.xxx.xxxxx/viewPDF #在線預覽pdf http://192.xxx.xxx.xxxxx/viewPDF2

7.5. 效果圖


八、項目源碼

gitlab地址:https://gitlab.com/gb-heima/file-online-preview

學習更多技術知識,請移步https://gblfy.com主頁

總結

以上是生活随笔為你收集整理的SpringBoot 使用LibreOffice 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。