springboot整合openoffice并在线预览

springboot整合openoffice

在这里插入图片描述

mcu项目需要对office文件上传并且在线编辑,于是找到和springboot整合的openoffice并且在线预览操作

一,下载openoffice并安装

1. 下载与安装Oppenoffice SDK

官方下载地址:https://www.openoffice.org/download/

二,运行openoffice服务

创建一个openoffice.bat文件,加入下面命令,保存双击执行

@echo off
cd C:\Program Files (x86)\OpenOffice 4\program
soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

三,添加pom依赖包

<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>4.2.2</version>
</dependency>

<!--springboot支持包,里面包括了自动配置类 -->
<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-spring-boot-starter -->
<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>

四,添加openoffice yml配置

jodconverter:
    local:
        enabled: true
        #office-home: C:\Program Files (x86)\OpenOffice 4   #安装路径
        max-tasks-per-process: 10
        port-numbers: 8100 #端口号

五,office文件转为pdf文件

注入:
    @Autowired
    private DocumentConverter converter;

    @GetMapping("/filetopdf")
    public void filetopdf(HttpServletResponse response) {
        /*需要转换的文件*/
        File file = new File("E:\\test\\1234.docx");
        try {
            // 转换之后文件生成的地址
            File newFile = new File("E:/test/pdf/");
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            // pdf文件生成保存的路径
            String savePath="E:/test/pdf/";
            String fileName="JCccc"+ UUID.randomUUID().toString().replaceAll("-","").substring(0,6);
            // pdf文件后缀
            String fileType=".pdf";
            // 将这三个拼接起来,就是我们最后生成文件保存的完整访问路径了
            String newFileMix=savePath+fileName+fileType;
            // 文件转化
            converter.convert(file).to(new File(newFileMix)).execute();
            // 使用response,将pdf文件以流的方式发送的前端浏览器上
            ServletOutputStream outputStream = response.getOutputStream();
            // 读取文件
            InputStream in = new FileInputStream(new File(newFileMix));
            // copy流数据,i为字节数
            int i = IOUtils.copy(in, outputStream);
            in.close();
            outputStream.close();
            System.out.println("流已关闭,可预览,该文件字节大小:"+i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

六,pdf在线预览

/**
 * 预览
 * @param response
 */
@GetMapping("/filetopreview")
public void filetopreview(HttpServletResponse response) {
    String pdfpath ="E:\\test\pdf\JCccc7a9291.pdf";
    try {
        // 使用response,将pdf文件以流的方式发送的前端浏览器上
        ServletOutputStream outputStream = response.getOutputStream();
        // 读取文件
        InputStream in = new FileInputStream(new File(pdfpath ));
        // copy流数据,i为字节数
        int i = IOUtils.copy(in, outputStream);
        in.close();
        outputStream.close();
        System.out.println("流已关闭,可预览,该文件字节大小:"+i);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

七,效果

在这里插入图片描述
参考网址:

https://blog.csdn.net/weixin_38977885/article/details/119649743

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值