参考地址
springboot:各种下载文件的方式_springboot下载文件-CSDN博客
开箱即用实战
@GetMapping("/t1")
public void down1(HttpServletResponse response) throws Exception {
response.reset();
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader(
"Content-disposition",
"attachment; filename=test.png");
try(
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Harbor\\Pictures\\Camera Roll\\111.jpg"));
// 输出流
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
){
byte[] buff = new byte[1024];
int len = 0;
while ((len = bis.read(buff)) > 0) {
bos.write(buff, 0, len);
}
}
}