@Autowired
private WebApplicationContext applicationContext;
@RequestMapping("/demo2")
public ResponseEntity<byte[]> demo2() throws IOException {
//固定获取服务器上某个文件的IO流。 通过 ServletContext对象。
ServletContext servletContext = applicationContext.getServletContext();
//搞到要下载的文件的io流。
byte[] input=FileUtils.readFileToByteArray(new File(servletContext.getRealPath("/file/spring-framework-reference.pdf")));
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
httpHeaders.set("Content-Disposition","attachment;filename=spring-framework-reference.pdf");
return new ResponseEntity<>(input, httpHeaders, HttpStatus.OK);
}
Spring MVC中的文件下载
最新推荐文章于 2024-07-09 20:20:12 发布