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

网友投稿 247 2023-01-09

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

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

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

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

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

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

com.aspose

aspose-slides

19.3

com.aspose

aspose-cells

8.5.2

com.aspose

aspose-words

15.8.0

把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文件路径

http:// 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

fileTZvTabkXoPdfComUtils.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("/")ZvTabkX;

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.前端代码:

onclick="showFile('d:/file/测试.doc')">预览doc

onclick="showFile('d:/file/测试.docx')">预览docx

onclick="showFile('d:/file/测试.xls')">预览xls

onclick="showFile('d:/file/测试.xlsx')">预览xlsx

onclick="showFile('d:/file/测试.pptx')">预览pptx

onclick="showFile('d:/file/数据库原理(第5版)(样章).pdf')">预览pdf

5.本人文件目录:

6.下载demo:

https://download.csdn.net/download/weixin_39220472/194ZvTabkX18676

总结

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决nacos升级spring cloud 2020.0无法使用bootstrap.yml的问题
下一篇:君泽国际快递物流查询单号(君泽国际快递物流查询单号是什么)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~