温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot2中怎么实现在线文档预览

发布时间:2021-06-18 16:49:42 来源:亿速云 阅读:177 作者:Leah 栏目:大数据

springboot2中怎么实现在线文档预览,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

第一步,需要引入相应的jar:

<!--在线文件预览================================================================-->
        <!--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>

第二步,在配置文件中加入关键配置:

springboot2中怎么实现在线文档预览

第三步:核心类

package com.yunji.kwxt.document;

import com.yunji.kwxt.common.enums.ResultEnum;
import com.yunji.kwxt.common.model.ResultJson;
import org.apache.commons.io.IOUtils;
import org.jodconverter.DocumentConverter;
import org.jodconverter.office.OfficeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * @author :LX
 * 创建时间: 2019/11/4. 17:42
 * 地点:广州
 * 目的: 在线文档预览
 *
 * 总的来说,这部分的代码是可以使用的,但是效果并没有预期的好,第一,中文乱码问题没解决,第二,格式并没有预期的好。
 * 业务逻辑这一块,不推荐这么弄,建议从源头控制上传文件,然后来预览操作。
 *
 * 如果后续需要使用,1 放开maven中的 jodconverter 包
 * 2 将 application-config.properties 文件相应 jodconverter 的配置放开
 * 3 将该类下面的 view 方法放开,调用 view 即可。
 *
 * 备注说明:
 */
@Controller
@RequestMapping("/doc")
public class DocumentController {

    private static Logger log = LoggerFactory.getLogger(DocumentController.class);

    @Resource
    private DocumentConverter documentConverter;


    /**
     * 在线预览
     * @param response
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    @ResponseBody
    public ResultJson view(HttpServletResponse response){
        //需要转换的文件
        File file = new File("E:\\下载\\kd.xlsx");
        //文件转换后的地址
        File toFile = new File("E:\\temp");
        if (!toFile.exists()){
            toFile.mkdirs();
        }

        ServletOutputStream outputStream = null;
        InputStream in = null;
        //关键方法,转换为PDF
        try {
            documentConverter.convert(file).to(new File("E:/temp/1.pdf")).execute();

            outputStream = response.getOutputStream();
            in = new FileInputStream(new File("E:/temp/1.pdf"));
            //将文件转换复制到流
            IOUtils.copy(in, outputStream);


        } catch (OfficeException e) {
            e.printStackTrace();
            log.error("转换文件失败");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("获取流失败");
        } finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "成功", null);
    }

}

看完上述内容,你们掌握springboot2中怎么实现在线文档预览的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI