OpenOffice实现文件在线预览

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
   <!--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>
package com.itheima.openoffice.controller;

import org.apache.commons.io.IOUtils;
import org.jodconverter.DocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/open")
public class OpenOfficeController {
    @Autowired
    private DocumentConverter documentConverter;

    @Value("${online.buffer.path}")
    private String bufferPath;

    @Value("${online.buffer.file.name}")
    private String bufferFileName;


    @GetMapping("/view")
    public void openOffice(HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        //获取需要转换的文件
        File file = new File("E:\\试一下.xlsx");
        //转换之后文件生成的地址
        File newFile = new File(bufferPath);
        if (!newFile.exists()) {
            newFile.mkdir();
        }
        try {
            documentConverter.convert(file).to(new File(bufferPath + bufferFileName)).execute();
            outputStream = response.getOutputStream();
            fileInputStream = new FileInputStream(new File(bufferPath + bufferFileName));
            IOUtils.copy(fileInputStream, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) fileInputStream.close();
                if (outputStream != null) outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/CECT151/article/details/127342011
今日推荐