1

I have a Rest API build with Spring MVC which has a controller

@RequestMapping(value = "/test", method = RequestMethod.POST)
public void postTest(@RequestBody FileUpload fileUpload) {
    System.out.println(fileUpload.getFilename());
    System.out.println(fileUpload.getFiletype());
    System.out.println(fileUpload.getFilesize());
}

My question is how do I put a size limit on the file being uploaded, so that my server cant be crashed with a upload of 1GB.

Vinchenzo
  • 572
  • 2
  • 7
  • 15
  • read the filesize in javascript and stop the upload there itself. – Guanxi Feb 03 '15 at 21:18
  • Yeah, but I want to close this hole on the server side, either in spring or tomcat. – Vinchenzo Feb 03 '15 at 21:22
  • I think this answers your question. See the `Tuning file upload limits` section https://spring.io/guides/gs/uploading-files/ – Svetlin Zarev Feb 03 '15 at 21:24
  • Yes this does work, but... This only works when the front end is sending a form. Im using angularjs to serialize the formdata to a json object with a content-type of application/json with the file upload being encoded to base64 and added to the object as a string. The json object would be sended in the body of the request. I would like the web server to have a limit on how big the body may be. – Vinchenzo Feb 04 '15 at 09:00
  • Hope this stack overflow link helps http://stackoverflow.com/questions/2943477/is-there-a-max-size-for-post-parameter-content – Kalyan Dec 16 '16 at 05:19

1 Answers1

0

If I am followig your qustion correction, you could put the file being uploaded into a Model object and check the size:

@RequestMapping(value="/upload", method=RequestMethod.POST)
public String processUploadWithModelAttribute(@ModelAttribute("myModelAttribute") 
final MyModelAttribute myModelAttribute, final BindingResult result,
 final Model model) throws IOException {        

    // CHECK FOR file.getSize()
}

You would need to create the MyModel Object with Multipart file in it.

Similar to How do I display validation errors about an uploaded multipart file using Spring MVC

Community
  • 1
  • 1
Paul John
  • 1,626
  • 1
  • 11
  • 15
  • 1
    But this would mean that the server first has to receive the complete file and then check for the size. That would make somebody be able to upload a 1GB file to my web server. I would like the webserver to cut off the connection as soon as it reads the post size of the request is more than xxMB – Vinchenzo Feb 04 '15 at 08:57
  • I see. I think you will be able to accomplish that by using http://commons.apache.org/proper/commons-fileupload/. Also there is a similar post http://stackoverflow.com/questions/14075287/does-maxpostsize-apply-to-multipart-form-data-file-uploads . Hope this helps. – Paul John Feb 04 '15 at 10:36
  • I also tried that solution bad it doesn't work well in my situations because im not uploading a MultiPartFile. What im doing is serializing the data sent from the front end in the request body. So it only contains text with the content-type set to application/json. – Vinchenzo Feb 04 '15 at 11:14
  • I am not sure what web server you are using. However, if Tomcat, there is a maxPostSize in Tomcat configuration? http://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html – Paul John Feb 04 '15 at 12:26
  • yeah im using tomcat and I tried the maxPostSize but It only works for application/x-www-form-urlencoded and multipart/form-data and not for sending the json data with application/json as content-type. – Vinchenzo Feb 04 '15 at 13:53