Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2009
    Posts
    32
    Rep Power
    0

    Default 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>

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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.

    Java 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");
      }
    }
    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 mb

Similar Threads

  1. File path validation in Java
    By aks123 in forum Advanced Java
    Replies: 2
    Last Post: 10-07-2009, 05:38 PM
  2. Jar resource file size
    By OrangeDog in forum Java Applets
    Replies: 1
    Last Post: 04-30-2009, 05:12 AM
  3. Set the Size of jvm.log file
    By kasidandu in forum New To Java
    Replies: 0
    Last Post: 03-16-2009, 05:08 PM
  4. Replies: 7
    Last Post: 10-28-2008, 02:41 PM
  5. File size
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-19-2007, 10:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •