Springboot 使用 OpenOffice 实现附件在线预览功能

OpenOffice 是 Apache 开源的一个办公组件,可以直接到官网下载使用。适用windows、linux、mac等各大平台,当然对我们程序员来说,肯定不会下载下来用用就完了。我们要在代码中使用她,实现一些 web 项目中的附件预览功能。

一、OpenOffice安装

安装的话就不细说了,直接到官网下载,一路next点下去就行,没啥难度。

二、新建boot项目

这里简单建一个springboot的项目,引入这几个转换包,

        <!-- jodconverter -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.3.0</version>
        </dependency>

随后,配置文件 application-dev.yml,

server:
  port: 8085

#jodconverter
jodconverter:
  local:
    enabled: true
    office-home: D:/dev/tools/OpenOffice 4
    max-tasks-per-process: 10
    port-numbers: 8100

三、Controller 调用

package com.my.cloud.previewservice.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.jodconverter.core.DocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @Author: 技术大咖秀
 * @Date: 2020/3/27 11:13
 * motto: Saying and doing are two different things.
 */
@Controller
public class PreviewController {

    @Autowired
    private DocumentConverter converter; // 转换器
    @Autowired
    private HttpServletResponse response;

    @RequestMapping("/home")
    public String homePage(){
        return "index";
    }

    @RequestMapping("/test")
    public String test(){
        return "test";
    }

    @RequestMapping("/toPdfFile")
    public String toPdfFile(){
        File file = new File("D:/test/excel.xlsx");//需要转换的文件
        try {
            File newFile = new File("D:/obj-pdf");//转换之后文件生成的地址
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            //文件转化
            converter.convert(file).to(new File("D:/obj-pdf/hello.pdf")).execute();
            //使用response,将pdf文件以流的方式发送的前段
            ServletOutputStream outputStream = response.getOutputStream();
            InputStream in = new FileInputStream(new File("D:/obj-pdf/hello.pdf"));// 读取文件
            // copy文件
            int i = IOUtils.copy(in, outputStream);
            System.out.println(i);
            in.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "This is to pdf";
    }

}

四、测试

新建测试文件,docx、xlsx、pptx 都试一下。

启动项目访问地址: http://localhost:8085/toPdfFile

测试成功。

PS。IE浏览器的话需要pdf.js(可百度下载)。

 

 

 

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值