记录下springboot中来进行文件下载
环境准备
导入依赖,没有使用传统的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-8
response.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中显示
测试结果
运行链接,得到结果