springboot中来进行文件下载

 2023-09-15 阅读 19 评论 0

摘要:导入依赖,没有使用传统的IO方式,使用了一个辅助类 <!--文件下载--><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><dependency

导入依赖,没有使用传统的IO方式,使用了一个辅助类

<!--文件下载--><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency>

JAVA代码部分

 @GetMapping("/uploadPDF")@ApiOperation(value = "下载PDF文件",notes = "下载PDF文件测试")public void uploadPDF(HttpServletResponse response) throws IOException {String fileName = "体检报告单.pdf";response.setContentType("application/pdf;charset=utf-8");//设置文件类型为为PDF,字符编码为UTF-8response.addHeader("Content-Disposition","attachment;fileName=" +new String(fileName.getBytes("UTF-8"),"iso-8859-1"));File file = new File("D:\\" + fileName);ServletOutputStream outputStream = response.getOutputStream();byte[] array = FileUtils.readFileToByteArray(file);outputStream.write(array);outputStream.flush();outputStream.close();}

在swagger中显示

在这里插入图片描述

运行链接,得到结果

在这里插入图片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/62715.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息