If the Tomcat server says “Client canceled” and the client says “Premature EOF”, who is right?

I have Tomcat streaming data to a Java client via http. It copies bytes from the file to the HTTPServletResponse output stream to the servlet.

The client uses HttpURLConnection to connect and read data.

Sometimes everything is fine, sometimes both the client and the server throw an exception.
The client says there is a "Premature EOF".
The server requires a "ClientAbortException".

Not only one of the above?

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 
+7
source share
2 answers

They are not mutually exclusive.

This situation can and will happen if the socket is unexpectedly closed. For example, consider what happens if your firewall just stops the socket. From the point of view of the server, when it tried to write data, the socket will be closed and a ClientAbortException will be thrown. From the client’s point of view, the next byte read will fail, leading to a premature exception.

+11
source

I had a similar problem and solved it without using BufferedReader , but at the same time reading one byte and putting the read in a try-catch for an EOFException. Hope this helps.

+1
source

All Articles