【发布时间】:2018-12-26 21:04:37
【问题描述】:
我的 REST 控制器中有这样一个方法,返回文件数据:
@RequestMapping(
value = "by-id/{attachmentId}",
method = RequestMethod.GET
)
public ResponseEntity<InputStreamResource> attachmentById(
@PathVariable("attachmentId") String attachmentId) {
GridFSDBFile file = service.getAttachment(attachmentId);
...... some unrelated code here, setting headers, etc .....
return new ResponseEntity<InputStreamResource>(
new InputStreamResource(file.getInputStream()), respHeaders, HttpStatus.OK);
}
这很好,但是根据 Fortify 的报告,我将发布 InputStream,显然在 file.getInputStream() 中打开。可能,我不得不使用 try-with-resources,因为 InputStream 是可自动关闭的,或者在 finally 块中调用 file.getInputStream().close()。但似乎我不能这样做,因为我完全不知道InputStreamResource的构造函数及其方法的实现,该输入流是否可能仍在返回的ResponseEntity中使用。
我该怎么办?
【问题讨论】:
标签: java spring-mvc file-io fortify