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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用Unoconv和LibreOffice进行格式转换实现在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件

發布時間:2024/9/27 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Unoconv和LibreOffice进行格式转换实现在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

此項目根據企業真實需求制作而成,希望能幫助大家解決在線預覽的問題!
此項目已開源,歡迎大家來STAR


軟件版本
SpringBoot2.2.2.RELEASE
LibreOffice6.3.2
unoconv0.6

文章目錄

  • 一、配置管理
    • ① pom
    • ② yml
    • ③ controller
    • ④ 文件格式轉換工具類FileFormatConverToPDF
    • ⑤ 在線預覽previewPDFUtils
    • ⑥ 啟動類
  • 二、測試驗證
    • ①測試鏈接
    • ②測試效果
  • 三、安裝Unoconv
    • ①yum安裝Unoconv
    • ②源碼安裝Unoconv
  • 四、安裝LibreOffice

一、配置管理

① 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.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gblfy</groupId><artifactId>business-online-preview</artifactId><version>0.0.1-SNAPSHOT</version><name>business-online-preview</name><url>https://gblfy.com</url><description>在線預覽</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

② yml

③ controller

package com.gblfy.onlinepreview.controller;import com.gblfy.onlinepreview.utils.FileFormatConverToPDF; 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 2020/01/08 8:09* @version1.0*/ @RestController public class FileOnlinePreviewController {/*** 在線預覽測試方法* 企業真實需求:* 文件的路徑 文件名 都需要動態獲取** @param response http響應網頁來實現在線預覽* @throws Exception*/@RequestMapping("/viewPDF")public void reviewWord(HttpServletResponse response) throws Exception {FileFormatConverToPDF linuxPageDIsplsyFileUtil = new FileFormatConverToPDF();//文件存儲路徑String fileStoragePath = "/app/ftpFileDir/testFileDir/businessLearning/";//轉換前的文件名String beforeConversion = "知識庫建設方案2019-11-11.docx";/*** 文件格式轉換+在線預覽*/linuxPageDIsplsyFileUtil.conversionFile(response, fileStoragePath, beforeConversion);} }

④ 文件格式轉換工具類FileFormatConverToPDF

package com.gblfy.onlinepreview.utils;import lombok.extern.slf4j.Slf4j; import org.apache.poi.util.IOUtils;import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.ArrayList; import java.util.List;/*** @Author:* @Date: 2019/1/15 0015 15:04* @describe 文檔在線預覽 (服務器環境為Linux環境) 目前文檔類型 僅開放 Excel 03/07 word 03/07 ppt 03/07*/ @Slf4j public class FileFormatConverToPDF {//libreoffice 文件格式轉換shell命令public static final String LIBREOFFICE_SHELLCMD = "/usr/bin/soffice --headless --invisible --convert-to pdf ";//unoconv 文件格式轉換shell命令public static final String UNOCONV_SHELLCMD = "/usr/bin/unoconv -f pdf ";//previewfile_dir 需要預覽的pdf目錄public static final String PREVIEWFILE_DIR = "/root/";private static FileFormatConverToPDF linuxPageDIsplsyFileUtil;public static synchronized FileFormatConverToPDF getSwitchUtil() {if (linuxPageDIsplsyFileUtil == null) {linuxPageDIsplsyFileUtil = new FileFormatConverToPDF();}return linuxPageDIsplsyFileUtil;}/*** 文檔在線預覽** @param response* @param fileStoragePath 文件存儲路徑 (前段獲取文件存儲路徑返給后臺)* @param beforeConversion 文件名(必須帶文件后綴名,這里指的就是文件全名稱)* @throws Exception*/public void conversionFile(HttpServletResponse response, String fileStoragePath, String beforeConversion) throws Exception {String fileNamePath = fileStoragePath + beforeConversion;log.info("文件路徑====" + fileNamePath);File file = new File(fileNamePath);if (!file.exists()) {log.info("庫存中沒有指定文件。。。。");return;}//獲取到文件名String interceptFileName = beforeConversion.substring(0, beforeConversion.lastIndexOf("."));//截取文件后綴名String fileNameSuffix = beforeConversion.substring(beforeConversion.lastIndexOf(".") + 1);String command = null;System.out.println("獲取到文件名====" + interceptFileName);System.out.println("截取文件后綴名====" + fileNameSuffix);if ("doc".equals(fileNameSuffix)|| "docx".equals(fileNameSuffix)|| "xls".equals(fileNameSuffix)|| "xlsx".equals(fileNameSuffix)|| "ppt".equals(fileNameSuffix)|| "pptx".equals(fileNameSuffix)) {System.out.println("此文件屬于" + fileNameSuffix + "開始進行轉換");command = LIBREOFFICE_SHELLCMD + fileNamePath;executeLinuxCmd(command);} else {command = UNOCONV_SHELLCMD + fileNamePath;executeCommand(command);}System.out.println("openPDF的參數====" + fileStoragePath + interceptFileName);previewPDFUtils.openPdf(response, PREVIEWFILE_DIR + interceptFileName + ".pdf");}/*** 使用LibreOffice進行格式轉換 to pdf** @param cmd* @return* @throws IOException*/public static List<String> executeLinuxCmd(String cmd) throws IOException {log.info("執行文件轉換的命令:" + cmd);Runtime run = Runtime.getRuntime();Process process = run.exec(new String[]{"/bin/sh", "-c", cmd});InputStream in = process.getInputStream();BufferedReader bs = new BufferedReader(new InputStreamReader(in));List<String> list = new ArrayList<String>();String result = null;while ((result = bs.readLine()) != null) {log.info("job result [" + result + "]");list.add(result);}in.close();process.destroy();return list;}/*** 使用Unoconv進行格式轉換 to pdf** @param command* @throws Exception*/private static void executeCommand(String command) throws Exception {log.info("執行文件轉換的命令:" + command);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);}} }

⑤ 在線預覽previewPDFUtils

package com.gblfy.onlinepreview.utils;import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream;/*** @author gblfy* @ClassNme previewPDF* @Description TODO* @Date 2020/1/8 12:47* @version1.0*/ public class previewPDFUtils {/*** 在線預覽pdf文件** @param response* @param previewFile 預覽pdf文件的絕對路徑* @throws Exception*/public static void openPdf(HttpServletResponse response, String previewFile) throws Exception {InputStream inputStream = null;OutputStream outputStream = null;System.out.println("進入openPDF=====" + previewFile);//String path ="/home/tubiao/桌面/優化數據庫.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();}} }

⑥ 啟動類

package com.gblfy.onlinepreview;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 在線預覽統一入口*/ @SpringBootApplication public class OnlinePreviewApplication {public static void main(String[] args) {SpringApplication.run(OnlinePreviewApplication.class, args);System.out.println("啟動成功!!!");} }

二、測試驗證

①測試鏈接

瀏覽器測試鏈接:http://localhost:8888/viewPDF

②測試效果


三、安裝Unoconv

①yum安裝Unoconv

unoconv 在線預覽 doc,doxc,xls,xlsx,ppt,pptx 文件功能環境搭建
https://gblfy.blog.csdn.net/article/details/102847276

②源碼安裝Unoconv

(企業內部)Linux環境_源碼安裝Unoconv實現文件在線預覽doc,doxc,xls,xlsx,ppt,pptx 文件
https://gblfy.blog.csdn.net/article/details/103540694

四、安裝LibreOffice

(企業內部)Linux環境_源碼安裝LibreOffice實現文件在線預覽doc,doxc,xls,xlsx,ppt,pptx 文件
https://gblfy.blog.csdn.net/article/details/103861905

友情鏈接:
(企業內部)SpringBoot 使用LibreOffice 在線預覽 doc,doxc,xls,xlsx,ppt,pptx 文件
https://gblfy.blog.csdn.net/article/details/103861607

總結

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

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