【问题标题】:Unreleased InputStream in InputStreamResource returned from REST controller从 REST 控制器返回的 InputStreamResource 中未释放的 InputStream
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-04-22
      • 2017-02-19
      • 2013-03-10
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多