File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File uploading

 
Vipin Sahu
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic