This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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".
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 ]