Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

简介: Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

1 介绍下OpenOffice

官网:https://www.openoffice.org/download/

Apache OpenOffice是一款先进的开源 办公软件套件,它包含文本文档电子表格演示文稿绘图数据库等。 它能够支持许多语言并且在所有普通计算机上工作。它将你所有的数据以国际开放标准格式存储下来,并能够读写从其它常用办公软件包来的文件。它可以被完全免费下载并使用于任何用途

2 安装OpenOffice

然后直接下一步安装就可以了,步骤过于简单这里省略,如有问题可以留言哈

3 Spring Boot整合

新建Spring Boot项目

3.1 依赖
<!--jodconverter 核心包 -->
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>4.2.2</version>
</dependency>
<!--springboot支持包,里面包括了自动配置类 -->
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-spring-boot-starter</artifactId>
    <version>4.2.2</version>
</dependency>
<!--jodconverter 本地支持包 -->
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local</artifactId>
    <version>4.2.2</version>
</dependency>
<!-- commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
3.2 配置文件
server.port=9999
#使能
jodconverter.local.enabled=true
#OpenOffice安装地址
jodconverter.local.office-home=C:/Program Files (x86)/OpenOffice 4
#同时执行任务的个数
jodconverter.local.max-tasks-per-process=10
#设置端口号(任意设置)
jodconverter.local.port-numbers=8110
#controller工具参数
online.buffer.path=C:/online_pdf/
online.buffer.file.name=hello.pdf
3.3 项目结构

3.4 编码

OnlineDocDao.java

/**
 * @desc: 模拟Dao层
 * @author: YanMingXin
 * @create: 2021/10/4-20:28
 **/
@Repository
public class OnlineDocDao {
    /**
     * 获取文件
     *
     * @param fileIndex
     * @return
     */
    @SuppressWarnings("all")
    public String getFiles(int fileIndex) {
        switch (fileIndex) {
            case 1:
                return "file1.docx";
            case 2:
                return "file2.docx";
            case 3:
                return "file3.docx";
            case 4:
                return "1.xlsx";
            default:
                return "file1.docx";
        }
    }
}

OnlineDocService.java

/**
 * @desc: Service接口
 * @author: YanMingXin
 * @create: 2021/10/4-20:27
 **/
public interface OnlineDocService {
    /**
     * 根据索引获取文件名称
     *
     * @param fileIndex
     * @return
     */
    String readDoc(int fileIndex);
}

OnlineDocServiceImpl.java

/**
 * @desc: Service实现类
 * @author: YanMingXin
 * @create: 2021/10/4-20:27
 **/
@Service
public class OnlineDocServiceImpl implements OnlineDocService {
    @Autowired
    private OnlineDocDao onlineDocDao;
    @Override
    public String readDoc(int fileIndex) {
        return onlineDocDao.getFiles(fileIndex);
    }
}

OnlineDocController.java

/**
 * @desc: 控制类
 * @author: YanMingXin
 * @create: 2021/10/5-9:50
 **/
@Controller
@SuppressWarnings("all")
public class OnlineDocController {
    /**
     * 注入DocumentConverter(jodconverter内置)
     */
    @Autowired
    private DocumentConverter converter;
    /**
     * 注入Service
     */
    @Autowired
    private OnlineDocService onlineDocService;
    @Value("${online.buffer.path}")
    private String bufferPath;
    @Value("${online.buffer.file.name}")
    private String bufferFileName;
    /**
     * 主要业务逻辑,处理文件请求
     *
     * @param index
     * @param response
     */
    @RequestMapping("/toPdfFile/{index}")
    public void toPdfFile(@PathVariable("index") Integer index, HttpServletResponse response) {
        String fileName = onlineDocService.readDoc(index);
        File file = new File("src/main/resources/static/" + fileName);
        ServletOutputStream outputStream = null;
        InputStream in = null;
        //转换之后文件生成的地址
        File newFile = new File(bufferPath);
        if (!newFile.exists()) newFile.mkdirs();
        try {
            //文件转化
            converter.convert(file).to(new File(bufferPath + bufferFileName)).execute();
            outputStream = response.getOutputStream();
            in = new FileInputStream(new File(bufferPath + bufferFileName));
            IOUtils.copy(in, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
                if (outputStream != null) outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3.5 启动测试

4 结语

4.1 为什么需要缓冲层?

4.2 OpenOffice端口号问题

因为看网上相同的文章的时候,端口号都写的8100,还以为OpenOffice的默认端口号是8100,但是改过了之后才发现,应该是Java连接OpenOffice时需要用到的进程端口号,可以随意设置,所以这个就不用管啦

相关文章
|
8天前
aspose实现word,excel等文件预览
aspose实现word,excel等文件预览
|
6天前
|
Java Apache 索引
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
|
8天前
|
Web App开发 前端开发 安全
如何用JAVA如何实现Word、Excel、PPT在线前端预览编辑?
随着信息化的发展,在线办公也日益成为了企业办公和个人学习不可或缺的一部分,作为微软Office的三大组成部分:Word、Excel和PPT也广泛应用于各种在线办公场景,但是由于浏览器限制及微软Office的不开源等特性,导致Word、Excel和PPT在在线办公很难整合到自己公司的OA或者文档系统。
380 2
|
8天前
|
Web App开发 JavaScript 前端开发
2024年纯前端VUE在线编辑微软Office/金山WPS的Word/Excel文档
现在,随着数字化进程渗透到到各行各业,数据安全已经成为了数字化革命中的重要组成部分,而在线Office成在OA、ERP、文档系统中得到了广泛的应用,为我国的信息化事业也做出了巨大贡献。随着操作系统、浏览器及Office软件的不断升级和更新换代,加上国家对信息化、数字化系统要求的不断提升,一些厂家的WebOffice控件产品不断被淘汰出局,而现存的几个产品也存在以下几个问题:
458 1
2024年纯前端VUE在线编辑微软Office/金山WPS的Word/Excel文档
|
8天前
|
Java Linux 数据安全/隐私保护
Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
136 0
|
8天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
65 0
|
8天前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
143 0
|
8天前
|
存储 JSON Java
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
50 2
|
8天前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
|
8天前
|
前端开发 Java 应用服务中间件
Springboot对MVC、tomcat扩展配置
Springboot对MVC、tomcat扩展配置