好久没写博客了,还是得动起来
最近项目有一个需求需要实现文件在线浏览,网上方案有很多,常见的有使用pdf.js,后面加文件路径,但仅限于pdf文件,还有就是使用openoffice把文件转换为pdf再去浏览,综合考虑,决定还是openoffice,下面实战
一、安装openoffice
首先当然是安装openoffice,官网下载地址: 传送门.
window下和linux下安装都有教程,window直接下一步,下一步,linux可以参考大佬博客传送门.
二、项目
文章最后有完整项目源码地址,我上传的,不要积分,免费提供
我们启一个简单的springboot项目,加入依赖的jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.2</version>
</dependency>
<!--jodconverter 本地支持包 -->
<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>
配置,需要配置本地openoffice地址端口等信息,我使用的是yml文件
jodconverter:
local:
office-home: C:\Program Files (x86)\OpenOffice 4
max-tasks-per-process: 10
port-numbers: 8100
enabled: true
写一个控制类
package com.example.dome.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.jodconverter.DocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/system/officeView")
public class OfficeViewController {
@Autowired
private DocumentConverter converter;
@Autowired
private HttpServletResponse response;
@RequestMapping(value = "/viewWord", method = RequestMethod.GET)
public String toPdfFile() {
File file = new File("C:\\Users\\guli\\Documents\\参数文档.docx");// 需要转换的文件
String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
try {
File newFile = new File("D:\\openoffice" + File.separator);// 转换之后文件生成的地址
if (!newFile.exists()) {
newFile.mkdirs();
}
if (!new File("D:\\openoffice" + File.separator + fileName + ".pdf").exists()) {
// 文件转化
converter.convert(file).to(new File("D:\\openoffice" + File.separator + fileName + ".pdf")).execute();
}
// 使用response,将pdf文件以流的方式发送的前段
ServletOutputStream outputStream = response.getOutputStream();
InputStream in = new FileInputStream(new File("D:\\openoffice" + File.separator + fileName + ".pdf"));// 读取文件
// copy文件
int i = IOUtils.copy(in, outputStream);
in.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return "Change PDF Over!";
}
}
比较核心的就converter.convert(file)这一段了,这个实际上是把我们要预览的文件转换成pdf格式输入到前端
三、访问
启动访问http://localhost:8080/system/officeView/viewWord
效果如图
当然我们可以使用pdf.js优化下页面,我的项目里面有这pdf.js的文件,大家可以直接使用
我们访问这个viewer.html传参即可
http://localhost:8080/html/web/viewer.html?file=http://localhost:8080/system/officeView/viewWord
记得是?file=
区别在于一个是浏览器自带的pdf预览,一个是使用pdf.js优化的预览,其实本质都差不都,只要你把文件转换为pdf了,咋看都行
四、注意
基本上大部分office的产品都可以支持,什么excel,word,图片都可以,就一种txt,中文无法解析,会是乱码,和格式无关,应该是解析那块源码有问题,我这边测试就这一种无法正常预览,大家可以搜相关博客,我记得有方案可以解决,不过要重新编译他的源码再打包,我就不搞了,看不了就下载就完了
就到这里了,有问题评论区见,期待三连哦!
五、源码
链接: 点击下载.