springboot利用aspose预览office文件的实现过程

 更新时间:2021年06月08日 09:12:10   作者:灰太狼_cxh  
这篇文章主要给大家介绍了关于springboot利用aspose预览office文件的相关资料,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考价值,需要的朋友可以参考下

springboot项目使用aspose预览office文件,运行实现预览效果:

主要实现原理是:浏览器可以直接预览pdf,所以使用aspose把office文件转换为pdf文件,进行预览。

1.主要写了个简单的demo,项目目录:

2.pom.xml添加aspose的依赖包

(目前maven仓库不提供以下aspose的依赖包,可以自行下载添加进maven仓库,或者直接拉到最下面下载本人demo,demo提供了相应的jar包)

<!--aspose预览office文件-->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-slides</artifactId>
            <version>19.3</version>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-cells</artifactId>
            <version>8.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.8.0</version>
        </dependency>
        <!--end-->

把jar包放在d盘,然后cmd,执行命令把jar包加进maven仓库

mvn install:install-file -Dfile=D:\jar\aspose-words-15.8.0.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar
 
 
mvn install:install-file -Dfile=D:\jar\aspose-cells-8.5.2.jar -DgroupId=com.aspose -DartifactId=aspose-cells -Dversion=8.5.2 -Dpackaging=jar
 
 
mvn install:install-file -Dfile=D:\jar\aspose.slides-19.3.jar -DgroupId=com.aspose -DartifactId=aspose-slides -Dversion=19.3 -Dpackaging=jar

3.后端主要代码:

@Controller
public class FileController {
 
    @Autowired
    private FileToPdfComUtils fileToPdfComUtils;
 
    /**
     * index页面
     * @return
     */
    @GetMapping("/index")
    public String index(){
        return "index";
    }
 
    /**
     * 文件预览
     * @param filePath
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/showFile")
    @ResponseBody
    public void showFile(String filePath, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //源文件路径
        String sourcePath = filePath;
        //pdf文件路径
        String pdfPath = null;
        //获取文件后缀判断是否转换pdf
        int index = filePath.lastIndexOf(".");
        String fileType = filePath.substring(index + 1);
        String toPdfSuffix = "doc,docx,xls,xlsx,ppt,pptx";
        try {
            if(toPdfSuffix.indexOf(fileType) >= 0){
                pdfPath = sourcePath.substring(0, index) + ".pdf";
                //源文件转换pdf
                fileToPdfComUtils.officeToPdf(sourcePath, pdfPath);
                File pdfFile = new File(pdfPath);
                InputStream is = new FileInputStream(pdfFile);
                showPdf(is, pdfPath, request, response);
            } else {
                //不用转换,直接预览
                File pdfFile = new File(filePath);
                InputStream is = new FileInputStream(pdfFile);
                showPdf(is,filePath, request, response);
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            //最后删除生成的pdf文件
            FileUtils.deleteFile(pdfPath);
        }
 
    }
 
    /**
     * 文件预览
     * @param is
     * @param fileKey
     * @param request
     * @param response
     * @throws IOException
     */
    public void showPdf(InputStream is,  String fileKey, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //根据文件名获取 MIME 类型
        int idx = fileKey.lastIndexOf(".");
        String suffix = fileKey.substring(idx);
        String[] fileKeys = fileKey.split("/");
        String fileName = fileKeys[fileKeys.length - 1];
        String contentType = FileContentType.SUFFIX_TYPE.get(suffix);
        //inline表示直接预览
        String userAgent = request.getHeader("USER-AGENT");
        String contentDisposition = "";
        if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent, "Trident") || StringUtils.contains(userAgent,"Edge")){
            //IE 浏览器
            contentDisposition = "inline;filename=" + URLEncoder.encode(fileName,"UTF8");
        }else {
            //其他浏览器
            contentDisposition = "inline;filename=" + new String(fileName.getBytes("UTF-8"),"ISO8859-1");
        }
        // 设置头
        response.setHeader("Content-Disposition",contentDisposition);
        response.setContentType(contentType);
        // 获取绑定了客户端的流
        ServletOutputStream output = response.getOutputStream();
        // 把输入流中的数据写入到输出流中
        IOUtils.copy(is,output);
        is.close();
    }
}

4.前端代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>预览文件</title>
</head>
<body>
<button target="_blank" type="button"
        onclick="showFile('d:/file/测试.doc')">预览doc </button>
<button  target="_blank" type="button"
        onclick="showFile('d:/file/测试.docx')">预览docx </button>
<button  target="_blank" type="button"
        onclick="showFile('d:/file/测试.xls')">预览xls </button>
<button target="_blank" type="button"
        onclick="showFile('d:/file/测试.xlsx')">预览xlsx </button>
<button  target="_blank" type="button"
        onclick="showFile('d:/file/测试.pptx')">预览pptx </button>
<button  target="_blank" type="button"
        onclick="showFile('d:/file/数据库原理(第5版)(样章).pdf')">预览pdf </button>
<script>
    //预览
    function showFile (filePath){
        var url = "/showFile" + "?filePath=" + filePath;
        window.open(url);
    };
 
</script>
</body>
</html>

5.本人文件目录:

6.下载demo:

https://download.csdn.net/download/weixin_39220472/19418676

总结

到此这篇关于springboot利用aspose预览office文件的文章就介绍到这了,更多相关springboot预览office文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Spring MVC4 纯注解配置教程

    详解Spring MVC4 纯注解配置教程

    本篇文章主要介绍了Spring MVC4 纯注解配置教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • java中TCP实现回显服务器及客户端

    java中TCP实现回显服务器及客户端

    本文主要介绍了java中TCP实现回显服务器及客户端,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Java多线程CAS操作原理代码实例解析

    Java多线程CAS操作原理代码实例解析

    这篇文章主要介绍了Java多线程CAS操作原理代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Spring的refresh()方法相关异常解析

    Spring的refresh()方法相关异常解析

    这篇文章主要介绍了Spring的refresh()方法相关异常解析,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • springboot3.x中Jakarta包无法引入的问题

    springboot3.x中Jakarta包无法引入的问题

    最近想将一些项目升级到springboot3.x和java17的时候,发现项目依赖中有Jakarta的包,但是代码标红提示没有相关的类,本文就来介绍一下解决方法,感兴趣的可以了解一下
    2024-02-02
  • Spring Boot配置内容加密实现敏感信息保护

    Spring Boot配置内容加密实现敏感信息保护

    之前我们讲过的配置相关知识都是Spring Boot原生就提供的,而今天我们将介绍的功能并非Spring Boot原生就支持,但却非常有用:配置内容的加密
    2021-11-11
  • 详解Spring中bean实例化的三种方式

    详解Spring中bean实例化的三种方式

    本篇文章主要介绍了详解Spring中bean实例化的三种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 一文搞懂Spring中Bean的生命周期

    一文搞懂Spring中Bean的生命周期

    这篇文章主要为大家介绍了Spring中Bean生命周期的使用及控制,文中的示例代码讲解详细,对我们学习或工作有一定的参考价值,感兴趣的可以学习一下
    2022-06-06
  • idea2019.1.4 鼠标放到方法上显示注解的实现操作

    idea2019.1.4 鼠标放到方法上显示注解的实现操作

    这篇文章主要介绍了idea2019.1.4 鼠标放到方法上显示注解的实现操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • java  HashMap和HashTable的区别详解

    java HashMap和HashTable的区别详解

    这篇文章主要介绍了java HashMap和HashTable的区别详解的相关资料,需要的朋友可以参考下
    2016-12-12

最新评论