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=jarmvn install:install-file -Dfile=D:\jar\aspose-cells-8.5.2.jar -DgroupId=com.aspose -DartifactId=aspose-cells -Dversion=8.5.2 -Dpackaging=jarmvn 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 {@Autowiredprivate FileToPdfComUtils fileToPdfComUtils;/*** index页面* @return*/@GetMapping("/index")public String index(){return "index";}/*** 文件预览* @param filePath* @param request* @param response* @throws IOException*/@GetMapping("/showFile")@ResponseBodypublic void showFile(String filePath, HttpServletRequest request, HttpServletResponse response) throws IOException {//源文件路径String sourcePath = filePath;//pdf文件路径String pdfPath = null;//获取文件后缀判断是否转换pdfint 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";//源文件转换pdffileToPdfComUtils.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文件相关推荐

  1. springboot使用pdfjs预览office文件

    由于springboot使用aspose预览office文件可以实现文件预览,但部分浏览器却不兼容,所以使用pdfjs预览office文件,兼容浏览器. 在springboot使用aspose预览of ...

  2. 在线预览office文件

    通过微软公开的api接口,将文档的URL传入即可实现在线预览office文件,而不需要去下载文件. 同时,若是想做提供预览office办公软件的服务,直接调用接口即可,无需利用openoffice或者 ...

  3. 微软接口在线预览office文件

    通过微软公开的api接口,将文档的URL传入即可实现在线预览office文件,而不需要去下载文件. 同时,若是想做提供预览office办公软件的服务,直接调用接口即可,无需利用openoffice或者 ...

  4. 通过微软官方接口预览office文件的js

    通过微软官方接口预览office文件的js //预览office function office(url){//文件名filename = url;var uri = url.replace(&quo ...

  5. 在线预览Office文件【效果类似百度文库】(转载)

    转载:http://www.cnblogs.com/yxlblogs/p/4139167.html 引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前项目中是用到公司购买 ...

  6. 如何压缩并预览Office文件(Word、PowerPoint和Excel)

    打开BetterZip,简洁的界面如下图所示.分为三个部分,左侧边栏显示文件所在的文件夹,中间部分即为需要***作的文件或压缩包,右侧边栏显示的是文件的相关信息. 1.如何快速将Word.PPT.Ex ...

  7. js在线预览office文件的示例代码

    方法一: 用微软的office online进行在线预览 https://view.officeapps.live.com/op/view.aspx?src=文件地址 只能查看 'doc', 'doc ...

  8. vue实现在线预览office文件

    最近在做电子档案,后端提供了文件的华为云的oss链接.已经实现了点击下载文件的功能.但是呢,他们又希望常规的文件,可以直接点击预览,不需要下载. 按道理说,做文件的在线预览,买个第三方服务什么的,后端 ...

  9. Filerun预览office文件中文字体乱码

    解决方法,向filerun的docker容器内添加Windows下常用字体文件 docker exec 连接到容器内部 在/usr/share/fonts/truetype/文件夹下新建一个字体目录, ...

最新文章

  1. mysql中date转sqlserver_MySQL和SQLServer互转
  2. 厉害了,史上最 “污” 技术解读。。
  3. 左神算法:用一个栈实现另一个栈的排序(Java版)
  4. XCTF_Web_新手练习区:weak_auth
  5. php 通知数据库,如何通过php通知我的iPhone应用程序名称已添加到数据库?
  6. RTMP/RTSP推送端和RTMP/RTSP播放端录像设计探讨
  7. yolov5 deepsort 行人车辆 双向计数 跟踪检测
  8. 最强AlphaGo怎样炼成?刚刚,DeepMind团队进行了全面解读
  9. js+css3实现旋转效果
  10. 分形之城:递归超典型例题,还没明白?手把手画给你看!
  11. 问题:双击Excel文件提示文件找不到,只打开程序不同时打开文件,但通过打开对话框却能打开文件。
  12. python udp
  13. 信道估计之MMSE算法
  14. R语言实现随机森林代码
  15. JavaScript基础之函数截流、防抖、柯理化
  16. Worktile 中百万级实时消息推送服务的实现
  17. AutoCAD中ObjectARX C++常用的方法
  18. 马达驱动 DRV8832
  19. Google Earth Engine ——MOD11A1/A2 V6产品Emis_31和32波段下载
  20. [视觉模型]迁移学习之五个步骤

热门文章

  1. dubbo + zookeeper学习
  2. Java获取微信access_token
  3. 智能家居实训(华清远见)第一天
  4. 特殊情况下需要分享屏幕? 基于 flask 的微型屏幕共享服务器(可实现屏幕共享)
  5. Win10热点无法连接使用
  6. 苹果home键失灵_Home键不能用?自行排查不行再送修!
  7. 利用彩虹表破解Hash
  8. day57-day58【代码随想录】二刷数组
  9. 第三节 数组的指针和指向数组的指针变量
  10. 天津理工C期末试题语言,天津理工大学C语言上机-题库.doc