SpringBoot 使用unoconv 在线预览 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 |
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 文件功能环境搭建
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*/
/*** @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. 放置文件
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. 效果图
八、项目源码
gitlab地址:https://gitlab.com/gb-heima/file-online-preview
学习更多技术知识,请移步https://gblfy.com主页
SpringBoot 使用unoconv 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件相关推荐
- SpringBoot 使用LibreOffice 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件
接上一篇:linux环境源码安装unoconv Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件 https://gblfy.blog. ...
- doc, docx, xls, xlsx, ppt, pptx,txt。等文件转化为pdf
需要OpenOffice第三插件的支持 ,支持window\linux\mac等系统.doc", "docx", "xls", "xlsx& ...
- unoconv 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件功能环境搭建
接上一篇: SpringBoot 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件 https://blog.csdn.net/weixin_40816738/article/de ...
- 微信小程序 - 在线预览 Office 文件(doc / docx / xls / xlsx / ppt / pptx / pdf)
效果图 前言 网上大部分教程功能有问题且文章无逻辑混乱,本文将提供优秀的示例. 本文只适用于预览 服务端接口返回的网络地址文件,"本地上传" 文件并预览原理一样, 例如服务端接口返 ...
- 使用Unoconv和LibreOffice进行格式转换实现在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件
此项目根据企业真实需求制作而成,希望能帮助大家解决在线预览的问题! 此项目已开源,欢迎大家来STAR 软件 版本 SpringBoot 2.2.2.RELEASE LibreOffice 6.3.2 ...
- Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件
因业务需求需要,用unoconv就可以轻松地实现利用LibOffice可以打开的文档的转换. 服务器版本 环境 系统版本 Linux Red Hat Enterprise Linux Server r ...
- java文件预览_java 在线预览doc,pdf
先说一说如何实现在线预览doc网上查了很多资料,基本思路就是将doc 转为 pdf,由于低版本浏览器不支持预览pdf,所以基本是再将pdf 转为 swf. 由于我这次做的需求只需要兼容chrome即可 ...
- java 文件在线预览_java 在线预览doc,pdf
先说一说如何实现在线预览doc 网上查了很多资料,基本思路就是将 doc 转为 pdf,由于低版本浏览器不支持预览 pdf,所以基本是再将 pdf 转为 swf (使用FlexPaper + swft ...
- 在线预览doc,docx文档
在线预览doc,docx文档 前言:上传成功以后的每个文档都能获取到所传文件的路径; 我这里是一个maven项目,需要在pom文件引入 <!-- 文件预览 --><dependenc ...
最新文章
- 使用Python,OpenCV进行去水印,图像修复
- 在Mac上通过VMware Fushion 15.1配置静态IP虚拟机实录
- python游戏程序-python游戏程序
- libevent中的信号处理
- 项目拆分子工程(简单版)
- 作为大龄开发人员,敢问路在何方?
- ArrayBlockingQueue原理分析-put方法
- linux磁盘信息文件,Linux查看硬盘信息方法总结归纳
- 面试官问:来实现一个Promise
- 写在前面-2015.11.30
- hihocoder1089 Floyd算法
- bzoj3124 [Sdoi2013]直径 直径+树形dp
- 前程无忧涉及网上黑市贩卖简历 盘前跌近5%
- DeFi货币市场协议DMM宣布因监管部门的要求,已停止运营
- 等级考试(三):三级网络---似曾相识(续)
- 终于将 SQL Server 成功迁移至 MySQL8.0 啦!!!
- rdkit GetAtoms获取化合物每个位置的索引;rdkit FindMCS大公共相同结构 找不同化合物之间的差异
- PCWorld:流量日趋集中 大公司影响整个互联网
- inode客户端linux 怎样运行,H3C_iNode智能客户端安装指导(Linux)
- android studio开发app设置登录界面
热门文章
- 过年,你肯定会用到这款小程序!
- scrapy爬取天气存MySQL_Scrapy实战篇(五)之爬取历史天气数据
- java编码规范右大括号换行_java编码规范摘选
- Android Activity 生命周期中onStart()和onResume()的区别
- java中集合的迭代操作
- ndoe.js实战之开发微博第一讲之工具准备
- 排序算法总结与C代码
- windows + cmake + vs2019 编程
- 用 Mars Remote API 轻松分布式执行 Python 函数
- 后疫情时代,银行从数字化转型到智能化“迁徙”