0
点赞
收藏
分享

微信扫一扫

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


接上一篇:linux环境源码安装unoconv
Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件​

接上一篇:linux环境yum安装unoconv
unoconv 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件功能环境搭建​

实现原理:在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

unoconv

0.6

LibreOffice

5.3.6.1 30(Build:1)

二、需求案例

实现 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 文件功能环境搭建​​

五、创建SpringBoot项目

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


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

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

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


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

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


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

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
*/
/**
* @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 = "/usr/bin/unoconv -f 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. 放置文件

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

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

7.2. 运行项目

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

7.3. 查看控制台日志

tail -f msg.log

7.4. 浏览器访问验证

#在线预览docx
http://192.168.45.56/viewPDF
#在线预览pdf
http://192.168.45.56/viewPDF2

7.5. 效果图

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


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

八、项目源码

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

学习更多技术知识,请移步​​https://gblfy.com​​主页


举报

相关推荐

0 条评论