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

File uploading

Vipin Sahu

Joined: Mar 08, 2007
Posts: 6
I am using validator framework for validation. File uploading functionality is working fine. I have specified max file size in config file. If file size is greater than specified size then how to display proper error like "file should not be greater than xyz size".

Please suggest how to solve this problem.

Thanking you in an anticipation.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, it's important to understand that the max file size you specify in the struts-config.xml file exists mainly for protection against denial of service attacks, where someone might try to upload a huge file just to overload your server and make it crash. If a user tries to upload a file larger than the size you specify, Struts simply won't upload the file, and won't populate the property associated with the file. If you try to access it in your Action class, it will simply be null. If the user just doesn't put anything in the file upload text box and presses the submit button, Struts will put a FormFile object in the ActionForm, but the getFileSize() method will return zero.

You could catch this error in the Validation framework by putting a "required" dependency on the file to be uploaded. However, this would give the wrong message. It would tell the user that the file is required, when the actual problem is that the file is too big.

What I'd suggest is just to have your action class check to see if the FormFile object is null. If it is, handle it the way you would any error message generated from an action class: Instantiate an ActionMessages object, add an ActionMessage to it, and call the saveErrors() method. Example:

[ March 09, 2007: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
I agree. Here's the link:
subject: File uploading
It's not a secret anymore!