仅当没有其他特定的资源实现适用时才应使用。特别是,尽可能选择ByteArrayResource或任何基于文件的Resource实现。
@RequestMapping(path = "/download", method = RequestMethod.GET)public ResponseEntity<Resource> download(String param) throws IOException { // ... InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); return ResponseEntity.ok() .headers(headers) .contentLength(file.length()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(resource);}Option2作为InputStreamResource的文档建议-使用ByteArrayResource:
@RequestMapping(path = "/download", method = RequestMethod.GET)public ResponseEntity<Resource> download(String param) throws IOException { // ... Path path = Paths.get(file.getAbsolutePath()); ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path)); return ResponseEntity.ok() .headers(headers) .contentLength(file.length()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(resource);}


