为什么在使用“response.flushBuffer()"提交响应后我仍然可以发送数据? [英] Why I can still send data after the response is commited with "response.flushBuffer()"?

查看:41
本文介绍了为什么在使用“response.flushBuffer()"提交响应后我仍然可以发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下 REST 处理程序将文件传输到客户端.

I use the following REST handler to transfer a file to client.

故意调用了response.flushBuffer(),希望我应该被阻止写入响应,因为它已经提交.我希望客户不会收到完整的文件.

I deliberately called response.flushBuffer() in hope that I should be blocked from writing to the response since it is already committed. And I expect client will not receive the full file.

但是整个文件仍然被发送到客户端.并且没有关于 Response Already Committed 的异常被抛出.

But the whole file still gets sent to client. And no exception about Response Already Committed is thrown.

为什么?

我的代码:

@RestController
public class ChunkedTransferAPI {

    @Autowired
    ServletContext servletContext;

    @RequestMapping(value = "bootfile.efi", method = { RequestMethod.GET, RequestMethod.HEAD })
    public void doHttpBoot(HttpServletResponse response) {

        String filename = "/realbootfile.efi";
        try {
            ServletOutputStream output = response.getOutputStream();

            InputStream input = servletContext.getResourceAsStream(filename);
            BufferedInputStream bufferedInput = new BufferedInputStream(input);
            int datum = bufferedInput.read();
            while (datum != -1) {
                output.write(datum);
                datum = bufferedInput.read();
                response.flushBuffer(); // <======= HERE
            }
            //output.flush();
            output.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

推荐答案

当标头已经发送时,响应被提交",这意味着对响应标头的更改不能被接受.它不会阻止您编写响应正文.

A response is "committed" when the header has been sent, and it means that changes to response headers cannot be honored. It doesn't stop you from writing the response body.

这篇关于为什么在使用“response.flushBuffer()"提交响应后我仍然可以发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆