1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@GetMapping(value = "/getFile", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ApiOperation(value = "根据id获取文件")
public void getFile(@RequestParam PoiTypeEnum type, @RequestParam Integer fileId, HttpServletResponse response) throws IOException {
FileEntity fileEntity = getById(id);
response.reset();
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setCharacterEncoding("UTF-8");
String utf8FileName = URLEncoder.encode(fileEntity.getFileName(), StandardCharsets.UTF_8);
response.setHeader("Content-Disposition", "attachment;filename=" + utf8FileName);
File file = new File(systemConfig.getStoragePath(), fileEntity.getFilePath());
try (ServletOutputStream outputStream = response.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);) {
int count = 0;
byte[] by = new byte[1024];
//通过response对象获取OutputStream流
while ((count = fileInputStream.read(by)) != -1) {
outputStream.write(by, 0, count);
}
outputStream.flush();
}
}
|