File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Struts & Commons File Upload & maxFileSize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts & Commons File Upload & maxFileSize" Watch "Struts & Commons File Upload & maxFileSize" New topic

Struts & Commons File Upload & maxFileSize

Steve Ford

Joined: Apr 19, 2004
Posts: 20
I have found some strange behavior when uploading multiple files through a web form using, multiple instances org.apache.struts.upload.FormFile . As you may, or may not know, under the hood Struts uses a CommonsMultipartRequestHandler to process forms of type multipart/form-data, and this class is essentially a wrapper around the Commons File Upload classes that do the work. The Struts config has a property on the controller tag called "maxFileSize". All Struts documentation suggests that this is the maximum file size that can be uploaded, and when uploading one file the behavior is exactly as you would expect - MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED gets set to true, etc, etc. The problem arises when uploading multiple files. The result is that the "maxFileSize" parameter ends up applying to the combined size of all the files. Given the documentation and the name of the property "maxFileSize", I would think it would be much more logical (and useful) if the max size applied to each individual file. Without going into too much detail, when you trace through the Struts and Commons File Upload code, ultimately the "maxFileSize" is compared to HttpServletRequest.getContentLength() to see if the upload is too big. Obviously this will take into account the combined file sizes, so I wouldn't call this a bug, but I don't think the behaviour makes sense. What are your thoughts?
Mauro Marra

Joined: Nov 15, 2005
Posts: 2
I came up to the same conclusions.
I'm doing an application with nested beans, where the client can upload one or more file depending on how many documents bean are nested in my Form.
So after a post I go trough the collection of form files to check the size of each file individually.
It works, but I don't know if there is a better way to do this.

What do you think
Steve Ford

Joined: Apr 19, 2004
Posts: 20
I think what you have described is the only way to do it. Keep in mind that you will need to set the maxFileSize in the struts config, equal to your "true" max file size (ie. the size you are checking for in your form files) multiplied by the maximum number of allowable uploads. If you don't do this, the controller may kill the form submission before you even get to your code which checkes file sizes.
I agree. Here's the link:
subject: Struts & Commons File Upload & maxFileSize
It's not a secret anymore!