简体   繁体   English

StreamingResponseBody 返回空主体

[英]StreamingResponseBody returning empty body

I have a simple Spring Boot controller that serves zip-files, which can get fairly large.我有一个简单的Spring Boot控制器,它提供 zip 文件,它可以变得相当大。 So in order to not having to load the entire file content into memory I'm creating an InputStream from the file to be read and return a StreamingResponseBody like this:因此,为了不必将整个文件内容加载到内存中,我正在从要读取的文件中创建一个InputStream并返回一个StreamingResponseBody如下所示:

import org.springframework.util.StreamUtils;

@RestController
@RequestMapping("/api/export")
public class ExportController {
        
    @GetMapping(value = "/{fileUuid}")
    public StreamingResponseBody exportFile(@PathVariable String fileUuid, HttpServletResponse response) {    
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"export.zip\"");
        InputStream inputStream = new FileInputStream(this.resolveFile(fileUuid));
        return outputStream -> {
            StreamUtils.copy(inputStream, outputStream);  // also tried inputStream.transferTo(outputStream);              
        };
    }

    public File resolveFile(String fileUuid) {
        // ...
    }
}

Now, this works fine for the very first request - but after that around 80% of requests are returned with an empty response body (the http-response just contains response headers).现在,这适用于第一个请求 - 但之后大约80% 的请求返回一个空的响应主体(http-response 只包含响应标头)。 We're using JDK11 and Spring Boot 2.4.3 .我们正在使用JDK11Spring Boot 2.4.3

What am I doing wrong here?我在这里做错了什么?


EDIT:编辑:

Strangely enough, I was able to solve the issue by wrapping the FileInputStream in a InputStreamResource and returning this resource instead of a StreamingResponseBody :奇怪的是,我能够通过将FileInputStream包装在InputStreamResource并返回这个资源而不是StreamingResponseBody来解决这个问题:

// ...
var resource = new InputStreamResource(new FileInputStream(this.resolveFile(fileUuid)));
return resource;

But I'm still curious of what's wrong with the original approach..但我仍然很好奇原来的方法有什么问题..

Had a similar problem and it turns out you might have a synchronous stream copy in filter happening before the asynchronous call filling the response outputStream.有一个类似的问题,事实证明,在异步调用填充响应 outputStream 之前,您可能在过滤器中发生了同步流复制。 Check if disabling other servlet filters makes it work.检查禁用其他 servlet 过滤器是否使其工作。 also additional logging will help in revealing the issue (check response data size, see whether it grows at some point in the filter chain).额外的日志记录也将有助于揭示问题(检查响应数据大小,查看它是否在过滤器链中的某个点增长)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM