0

与以小块直接写入 HttpServletResponse 输出流相比,分块编码有什么好处?

因此,执行如下所示的代码,其中完整的文件内容没有被缓冲在应用程序内存中,而不是使用分块编码头发送。我正在使用下面的 IoUtils.copy,它一次只能从输入流中读取 8K 字节。

@GetMapping("/content")
public void getContent(final HttpServletRequest request,
                        final HttpServletResponse response) throws IOException {
    try(final InputStream is = FileUtils.openInputStream(new File("src/main/resources/test_file.txt"))) {
        //Do something with is
        response.setHeader("ContentType", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setContentLength(70000);
        IOUtils.copy(is, response.getOutputStream());
        response.flushBuffer();
    }
}
4

0 回答 0