https://blog.csdn.net/shiboyuan0410/article/details/115209291
https://www.cnblogs.com/xulijun137/p/12210200.html zip压缩下载(大文件可能有问题)
https://my.oschina.net/pwh19920920/blog/4699776?tdsourcetag=s_pctim_aiomsg 下载大文件导致oom 内存溢出系统崩溃
https://blog.csdn.net/zhongguowangzhan/article/details/121773571
- 使用FileSystemResource,以文件系统的绝对路径的方式访问静态资源
FileSystemResource file= new FileSystemResource(“c:\xx\xxx\1.txt”);
@GetMapping(“/down”)
public ResponseEntity
File file = new File(uri);
if (!file.isFile()) {
throw new ServiceException(“文件不存在”);
}
String filename = FilenameUtils.getName(uri);
HttpHeaders headers = new HttpHeaders();
headers.add(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(filename, “UTF-8”));
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(new FileSystemResource(file), headers, status);
}
- 使用缓存流,边读边写
@GetMapping(“/down”)
public void download(@RequestParam(“uri”) String uri, HttpServletResponse response) {
File file = new File(uri);
if (!file.isFile()) {
throw new ServiceException(“文件不存在”);
}
String filename = FilenameUtils.getName(uri);
response.setHeader(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(filename, “UTF-8”));
try (FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream())) {
FileCopyUtils.copy(bufferedInputStream, bufferedOutputStream);
} catch(Exception e){
} finally {
}
}
@GetMapping(“/down”)
public void download(@RequestParam(“uri”) String uri, HttpServletResponse response) {
File file = new File(uri);
if (!file.isFile()) {
throw new ServiceException(“文件不存在”);
}
String filename = FilenameUtils.getName(uri);
response.setHeader(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(filename, “UTF-8”));
try (FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream())) {
byte[] buffer = new byte[1024];
int n = bufferedInputStream.read(buffer);
while(n != -1){
bufferedOutputStream.write(buffer);
n = bufferedInputStream.read(buffer);
}
bufferedInputStream.close();
bufferedOutputStream.close();
} catch(Exception e){
} finally {
}
}
3. 文件存储到oss或者是七牛云,绕过服务器下载等方式