问题描述
这是我在刷新 ServletOutputStream
时得到的一些例外。它不会在每个请求中发生,因此我无法重现它。
Here is the exception which I get some times while flushing ServletOutputStream
. It does not occur on every request so I am not able to reproduce it.
java.lang.NullPointerException
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
at org.apache.coyote.http11.InternalOutputBuffer.flush(InternalOutputBuffer.java:119)
at org.apache.coyote.http11.AbstractHttp11Processor.action(AbstractHttp11Processor.java:805)
at org.apache.coyote.Response.action(Response.java:174)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:366)
at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:333)
at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:101)
at darshan.HttpRequestHandler.returnResponse(HttpRequester.java:117)
HttpRequester.java:
private void returnResponse(HttpServletResponse response , String responseStr)
{
InputStream in = null;
ServletOutputStream out = null;
try
{
in = new ByteArrayInputStream(responseStr.getBytes("UTF-8"));
response.setContentLength(responseStr.length());
out = response.getOutputStream();
byte[] outputByte = new byte[4096];
while(in.read(outputByte, 0, 4096) != -1)
{
out.write(outputByte, 0, 4096);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
out.flush(); //<-- Exception at this line
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
此错误的解决方案是什么? ??
Any solution for this error???
推荐答案
您的代码无需调用 flush
或输入或输出流上的关闭
。 servlet容器将为您处理冲洗和关闭。
Your code doesn't need to call flush
or close
on the input or output streams. The servlet container will take care of flushing and closing for you.
有时会出现需要手动刷新呼叫的角落情况,但这不是其中之一。但是,在其中任何一个上调用关闭
都不是一个好主意,并且可能会在tomcat中触发导致异常的错误状态。
There are sometimes corner-cases where a manual flush call is required, but this isn't one of them. Calling close
on either of them, however, is really not a good idea, and may be triggering a bad state within tomcat that's causing your exception.
请参阅
这篇关于刷新ServletOutputStream时出现Nullpointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!