gpt4 book ai didi

java - 从 REST Controller 返回的 InputStreamResource 中未释放的 InputStream

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:32 26 4
gpt4 key购买 nike

我的 REST Controller 中有这样一个方法,返回文件数据:

@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 block 中调用 file.getInputStream().close() 。但似乎我不能这样做,因为我完全不知道 InputStreamResource 的构造函数及其方法的实现,也不知道该输入流是否仍在返回的 ResponseEntity 中使用。

我该怎么办?

最佳答案

我认为您已经能够找到问题的答案。而且,这可能是 Fortify 的一个问题,因为 Spring 关闭了流 - 请参阅“调查” - How to handle IO streams in Spring MVC

关于java - 从 REST Controller 返回的 InputStreamResource 中未释放的 InputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50173845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com