0

I've been looking around for something to break down my request when an exception is thrown. I have a file upload in my jsp which can't be more than 2MB. There is jsp validation but there has to be an extra safety for when internet explorer 8 or 9 is being used because the file API is not supported there or when javascript is being disabled client side.

I have a CommonMultipartResolver from spring which correctly handles my request when uploading a bigger file (it throws a MaxUploadSizeExceededException). When my ExceptionResolver handles the request and sends back a ModelAndView.

The real problem occurs when I upload a file which is for example 4GB. The exception is thrown by the multipartResolver, the exception is caught by the exceptionhandler and a modelAndView is trying to be returned. This trying to return the modelAndView takes ages to return because the request is being handled and the file has to be fully uploaded.

I want to break down my request and stop handling the file upload, someone with experience in doing this?

  • 1
    You can't determine the file size without streaming it up directly to server. The server has no way to access the client's file system. If you want to check file size and break down at client side , you need use client side file upload libraries such as [JQuery File Upload](https://github.com/blueimp/jQuery-File-Upload) – Cataclysm Mar 18 '15 at 08:51
  • have look [here](http://stackoverflow.com/questions/20162474/how-do-i-receive-a-file-upload-in-spring-mvc-using-both-multipart-form-and-chunk) and [here](http://creativejs.com/tutorials/advanced-uploading-techniques-part-1/) – Bond - Java Bond Mar 18 '15 at 08:59
  • I do not want jsp validation, I actually want java validation for extra security issues because you will always be able to bypass the frontend side of uploading a file. So I want to break down the request in stead of processing the whole file. The exception is thrown while processing the file but when I redirect to my original page or another (doesn't matter) it takes ages because the file still needs to be processed... – Bas Van Deuren Mar 18 '15 at 09:03
  • @Bond-JavaBond I can't use chuncked file upload because of IE8 and 9 don't support this. And this too can be bypassed by performing a restcall without the frontend... I need only java to cancel a request from processing and redirect. – Bas Van Deuren Mar 18 '15 at 09:08

1 Answers1

0

There is jsp validation but there has to be an extra safety for when internet explorer 8 or 9 is being used because the file API is not supported there or when javascript is being disabled client side.

It depends. Do you need to fully support people that block scripts and/or use ancient browsers?

If you can live with those cases just running into an error, you can block over sized requests before they hit your app server, i.e. with Apache's LimitRequestBody directive.

duckstep
  • 1,148
  • 8
  • 17
  • I do need to fully support these people for own safety, if a few people throw a file of 100GB or even more on the server it will be keeping processed and at a point my server 's memory will be full and the whole thing will crash... – Bas Van Deuren Mar 18 '15 at 09:06
  • _Fully support_ means error handling by your app, so you can return a nice error page. LimitRequestBody is enough to make your server safe, but the client side will see an error generated by the browser - afaik apache just terminates the connection (and sends a HTTP 4xx code? not sure, i've only used this to protect some SOAP services) in case the request is too large. – duckstep Mar 18 '15 at 09:34
  • I want to try this, but I have no clue how to get this working. I have a jboss running in java 6 with spring 3 and I want to upload a file in my webflow which is defined in xml config. My guess is you don't have enough information with this but I have not really an idea what you need to know to help me. I only want to specify this on a particular flowstep because I won't allow big files there, somewhere else there might be a big file upload. I don't have to set this in the startup property file from jboss am I? – Bas Van Deuren Mar 18 '15 at 09:46
  • Is your jboss listening on public IP, port 80 (unusual, but possible) or do you have some sort of reverse proxy (i.e. apache with ajp, nginx with proxy_pass) set up? If jboss listens on port 80 (clients connect directly to jboss), or you need give nice feedback to clients with no js or old IEs then you should read about chunked/multipart upload handling, as @Java Bond suggested. If you have a reverse proxy, blocking the request there will take some load off your jboss, because those requests won't even reach it. – duckstep Mar 18 '15 at 10:02
  • My jboss is listening on 8080 so no reverse proxy, my client connects directly to my jboss which want to pass the bytearray to my next step in the flow. But my multipartinvoker is intercepting this to check the bytearray size and throws an exception because the file is to big. After that, I want to immediatly return to my page without handling the rest of the request bytearray. The upload can not finish because I want to go back I just want to throw away my requestscope and start over from that flowstep. – Bas Van Deuren Mar 18 '15 at 10:06
  • I guess in that case your best option is to somehow adjust your flow to cancel the upload once it exceeds your limit and delete the partial upload byte array. Don't ask me how though, i have only very limited experience with spring. – duckstep Mar 18 '15 at 10:15
  • Yes I was hoping to do that. And actually that was why I asked this question to get to know how to delete the upload byte array or just how to quit the request from being processed. So not the whole file is being downloaded from the frontend. Still, thank you for your time and ideas :) – Bas Van Deuren Mar 18 '15 at 10:23