Java File Upload size Limitation
Posted on 2004-09-22
I am working on a servlet that will do file uploads, and all works fine. However I want to add a size limitation to it. I did this, and at first had it upload to the server, then check the size, and if it was too large, had it send back a message to the user saying it was too large.
Then I tried doing a check like this.
if (1048576 < req.getContentLength())
throw custom error message;
meaning that if the file was larger then 1mb they would get a custom error. This works, sort of. For small files (under a couple mb) it only takes a few seconds, however when I attempted to upload a 1.6gb file (as one of my clients tried to do) the browser sits there and hangs.
Is there a way to force the error to occur and stop parsing the user request? The first thing I have the service method do is that if statement. I get the error in my logs instantly, but not to the browser. Does the client browser get hung up on sending the file, even though I'm not doing anything with it?
I also tried instead of throwing a custom error, I threw an IOException, which didn't work.
I also tried to do a res.sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "File is larger than the set limit");
Which also didn't work. Is there any way to not have the clients browser choke on this?