-2

I m working on javaserver faces i had problem with checking the size of the image file uploaded ,can any one help me ?

2 Answers2

0

Not sure what your are using for file uploading but you could check 'content-length' header of uploaded file.

Or if your were using 'Apache commons fileupload' you could do it like so

   ServletFileUpload upload = new ServletFileUpload();
    upload.setFileItemFactory(new DiskFileItemFactory());
    List fileItems = upload.parseRequest(request);

    for(int i=0; i < fileItems.size(); i++){
       FileItem item = (FileItem)fileItems.get(i);
       if(item.isFormField() == true){
          if(item.getFieldName() !=null  ){
             int fileSize = item.get().length ;
          }
       }
    }
Greg
  • 1,671
  • 2
  • 15
  • 30
0

you can use java.io.File.length() for getting size.

getting file size by java.io.File.length() is ok but it is slow .instead it you can use fileChannel's size method.it is littlebit faster than java.io.File.length()

FileInputStream fis = null;
            try {
                File me = new File(your file path).getFile());
                fis = new FileInputStream(me);
                return fis.getChannel().size();
            } finally {
                fis.close();
            }
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70