调用有什么区别:
res.flushBuffer();
对比
res.getOutputStream().flush();
这些方法是否刷新相同的缓冲区?
如果是这样,你能告诉我这个缓冲区是如何由 servlet 容器管理的吗?
最佳答案
如果您一直在使用 getOutputStream
写入正文,它们将刷新相同的缓冲区。另一种选择是用于非二进制数据的 getWriter
。如果您一直在使用它,那么调用 res.getOutputStream().flush();
可能行不通。
缓冲区的管理方式是特定于实现的,但采用 one of the Tomcat implementations for example .可以看到有这样一些字段:
/**
* The associated output buffer.
*/
protected OutputBuffer outputBuffer;
/**
* The associated output stream.
*/
protected CoyoteOutputStream outputStream;
/**
* The associated writer.
*/
protected CoyoteWriter writer;
调用 getOutputStream()
创建一个 CoyoteOutputStream
,它使用那里显示的 outputBuffer
字段,同样适用于 getWriter()
。所以他们都会使用 outputBuffer
取决于你使用哪个。 flushBuffer
就是这样做的:
@Override
public void flushBuffer()
throws IOException {
outputBuffer.flush();
}
关于java - res.flushBuffer() 与 res.getOutputStream().flush();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13318024/