Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a HTTP connection from the HttpServlet

I'm running a servlet in Tomcat 6.0.26. The servlet accepts file upload from the client by HTTP POST. I'd like to stop the file uploading from the HttpServlet side. I tried the following methods with no luck:

  1. close the request inputstream
  2. send error code HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE and flush response
  3. do 1 and 2 in a Filter

I googled but found no direct answers. Please advise solutions.

Thanks.

like image 928
Jason Avatar asked Jun 24 '10 06:06

Jason


1 Answers

This is not possible using the standard Servlet nor Commons FileUpload API's. Basically, to be able to abort the connection immediately, you should grab the underlying socket physically and close it. However, this socket is controlled by the webserver. See also this related question: How to explicitly terminate http connection from server with no response header.

Little tests have however confirmed that Commons FileUpload doesn't buffer up the entire file in memory when its size exceeds the limit. It will read the input stream, but just ignore and throw away the read bytes (also the ones which are already read). So memory efficiency isn't necessarily the problem here.

To fix the real problem, you'd basically like to validate the file size in the client side rather than the server side. This is possible with a Java Applet or a Flash Application. For example, respectively JumpLoader and SWFUpload.

like image 109
BalusC Avatar answered Oct 22 '22 02:10

BalusC