使用ResponseEntity<byte[]>来实现文件下载。文件下载只需要将文件输出类型该为可以被下载的文件类型设置为ResponseEntity<byte[]>即可。
请求的具体方法如下
@RequestMapping("/downFile")
public ResponseEntity<byte[]> testdownFile(HttpSession session)
throws IOException {
ServletContext servletContext = session.getServletContext();
InputStream in = servletContext.getResourceAsStream("downloads/down.txt");
byte[] bytes = FileCopyUtils.copyToByteArray(in);
HttpHeaders header = new HttpHeaders();
header.add("Content-Disposition", "attachment;filename=down.txt");
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bytes, header, HttpStatus.OK);
return entity;
}