I use Spring MVC to implement file upload on the back-end, and the front end is simply a regular HTML
<form method="POST" enctype="multipart/form-data" action="/upload">
file to upload: <input type="file" name="file"><br>
<input type="submit" value="Upload" />
</form>
Everything works fine except if I close the browser during the upload, I will see the server throwing error(you have to interrupt the process earlier enough to see it)
java.io.EOFException: Unexpected EOF read on the socket
at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:152)
at org.apache.coyote.http11.InternalNioInputBuffer$SocketInputBuffer.doRead(InternalNioInputBuffer.java:177)
at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:110)
at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:416)
at org.apache.coyote.Request.doRead(Request.java:460)
at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:338)
at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:395)
...
I wonder if there is a better way to handle the exception or I can safely leave it as is?
There is no possibility to handle this exception in your code.
Also this exception does not harm as it only informs that the network socket is closed unexpectedly (before all content from the upload was received).
You can try to modify your log settings to suppress this exception but then it is possible that you miss other exceptions as well.