Results 1 to 2 of 2
Thread: validation on file size
- 10-23-2009, 10:47 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 32
- Rep Power
- 0
validation on file size
hi all
I want to validate file on its size. i have to write validation.xml for that action
what should be the parameter i should use the one i am doing is not working.
<validators>
<field name="upload">
<field-validator type=" ???">(i dont know what should i give it here)
<param name="size">2 MB</param>
<message key="Size cannot be more than 2 MB"/>
</field-validator>
</field>
</validators>
- 10-30-2009, 07:08 AM #2
the [struts] validators are only useful for all form fields except file upload field. uploaded files are usually handled by commons-fileupload, and the file contents are not automatically available to your bean. well, in the form class or action handler you could use code to get a reference on the file upload object and query the size and fail it there. , and in that case make the maximum size a configurable value in the struts-config.xml or other property file.
see also: struts file upload max size 5 mb,struts file upload max size 5 mb Tutorial, Tutorials struts file upload max size 5 mb,Example code struts file upload max size 5 mb, Java struts file upload max size 5 mbJava Code:public class StrutsUploadAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ StrutsUploadForm myForm = (StrutsUploadForm)form; // Process the FormFile FormFile myFile = myForm.getTheFile(); String contentType = myFile.getContentType(); String fileName = myFile.getFileName(); int fileSize = myFile.getFileSize(); byte[] fileData = myFile.getFileData(); System.out.println("contentType: " + contentType); System.out.println("File Name: " + fileName); System.out.println("File Size: " + fileSize); // validate size < 2MB if ( fileSize > 2000000) { return mapping.findForward("Error"); } return mapping.findForward("success"); } }
Similar Threads
-
File path validation in Java
By aks123 in forum Advanced JavaReplies: 2Last Post: 10-07-2009, 04:38 PM -
Jar resource file size
By OrangeDog in forum Java AppletsReplies: 1Last Post: 04-30-2009, 04:12 AM -
Set the Size of jvm.log file
By kasidandu in forum New To JavaReplies: 0Last Post: 03-16-2009, 04:08 PM -
How can you get the exact size of a file in bytes.
By J-Live in forum New To JavaReplies: 7Last Post: 10-28-2008, 01:41 PM -
File size
By eva in forum New To JavaReplies: 2Last Post: 12-19-2007, 09:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks