java 如果 Tomcat 服务器说“Client Aborted”,而客户端说“Premature EOF”,谁说的对?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4629704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If a Tomcat server says "Client Aborted", and the client says "Premature EOF", who is right?
提问by z5h
I have a Tomcat server streaming data to a Java client over http. It is copying bytes from a file to HTTPServletResponse's outputstream in a servlet.
我有一个 Tomcat 服务器通过 http 将数据流式传输到 Java 客户端。它将字节从文件复制到 servlet 中的 HTTPServletResponse 的输出流。
A client uses HttpURLConnection to connect and read the data.
客户端使用 HttpURLConnection 来连接和读取数据。
Sometimes all is fine, other times both the client and server throw an exception.
The client says there is a "Premature EOF".
The server claims "ClientAbortException".
有时一切正常,有时客户端和服务器都会抛出异常。
客户说有一个“过早的EOF”。
服务器声称“ClientAbortException”。
Isn't just one of the above possible?
是不是只有上述之一可能?
CLIENT:
客户:
java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.fastRead(ChunkedInputStream.java:234)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:662)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2669)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2664)java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.fastRead(ChunkedInputStream.java:234)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:662)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2669)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2664)
SERVER:
服务器:
ClientAbortException: java.io.IOException
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:358)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:434)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:349)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:381)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:370)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89)
...
Caused by: java.io.IOException
at org.apache.coyote.ajp.AjpAprProcessor.flush(AjpAprProcessor.java:1223)
at org.apache.coyote.ajp.AjpAprProcessor$SocketOutputBuffer.doWrite(AjpAprProcessor.java:1310)
at org.apache.coyote.Response.doWrite(Response.java:560)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:353)
... 23 more
回答by Rob Di Marco
They are not mutually exclusive.
它们并不相互排斥。
This situation can and will occur if the socket is closed unexpectedly. For example, consider what would happen if your firewall just terminated the socket. From the server's point of view, when it tried to write data, the socket would appear closed, and the ClientAbortException would be triggered. From the client's point of view, the next read of bytes would fail, causing the premature end exception.
如果套接字意外关闭,就会发生这种情况。例如,考虑如果您的防火墙只是终止了套接字会发生什么。从服务器的角度来看,当它尝试写入数据时,套接字将显示为关闭,并且会触发 ClientAbortException。从客户端的角度来看,下一次读取字节会失败,导致过早结束异常。
回答by Mat B.
I was having similar problem while ago and I solved it by not using BufferedReader
but reading one byte at a time and put the read in a try-catch for EOFException. Hope this helps.
前段时间我遇到了类似的问题,我通过不使用BufferedReader
而是一次读取一个字节并将读取的内容放入 EOFException 的 try-catch 中来解决它。希望这可以帮助。