3 Replies Latest reply on Mar 4, 2009 8:24 AM by andrei_exadel

    Why? FileUpload: uploadItem.getData() is null !

    allforjava

      Hi,

      I'm working on rich:fileUpload demo example from http://livedemo.exadel.com/richfaces-demo/richfaces/fileUpload.jsf?c=fileUpload&tab=usage.

      The event.getUploadItem().getData() throws null value instead file data. However, the UI shows as done status.

      I have NOT used any seam mulipartFilter in web.xml/component.xml.

      Please clarify why it occurs and what need to be done? Thank you in advance

      public synchronized void listener(UploadEvent event){
      
       UploadItem item = event.getUploadItem();
       File file = new File();
      
       try{
      
       System.out.println("ITEM: "+item.getData());
       file.setLength(item.getData().length);
       file.setName(item.getFileName());
       file.setData(item.getData());
       files.add(file);
       uploadsAvailable--;
       }
       catch(Exception e){
       System.out.println("ERROR:[listener()]: "+e);
       //System.out.println("UploadItem Size: "+item.getData().length);
       }
       }


        • 1. Re: Why? FileUpload: uploadItem.getData() is null !

          Set createTempFiles in FALSE in your web.xml:

          <filter>
          <filter-name>Seam Filter</filter-name>
          <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
          <init-param>
           <param-name>createTempFiles</param-name>
           <param-value>false</param-value>
           </init-param>
          </filter>


          • 2. Re: Why? FileUpload: uploadItem.getData() is null !
            allforjava

            Thank you andrei_exadel!

            The given code snippet works! The reason was because the org.richfaces.model.UploadItem sets/creates either java.io.File or byte[] depending on:

            <init-param>
             <param-name>createTempFiles</param-name>
             <param-value>false</param-value>
             </init-param>
            </filter>


            1. However the problem is, we could use either one of these and not the both simaltenously (size/purpose dependent). For example, in the rich:fileUpload demo we need to read file in bytes but I prefer to upload file, stored as temporary file.

            We have worked out the demo, as follows. Is it worth wile?

             public synchronized void listener(UploadEvent event){
            
             UploadItem item = event.getUploadItem();
             File file = new File(); // Caution: NOT a java.io.File
            
             try{
            
             file.setName(item.getFileName());
             file.setMime(item.getContentType());
            
             /*Get File Data*/
             if (item.isTempFile()) {
            
             byte[] fileInBytes = new byte[(int)item.getFile().length()]; //* TODO: Avoid loss of data in casting 'long' to 'int'
             java.io.File tempFile = item.getFile();
             FileInputStream fileInputStream = new FileInputStream(tempFile);
             fileInputStream.read(fileInBytes);
             fileInputStream.close();
            
             file.setData(fileInBytes); //
            
             } else {
             file.setData(item.getData());
             System.out.println("STATUS: "+"Not a temp File!");
             }
            
             file.setLength(file.getData().length);
             System.out.println("FILE LENGTH: "+file.getData().length/1024+"kb");
            
             files.add(file);
             uploadsAvailable--;
             }
             catch(Exception e){
             System.out.println("ERROR:[listener()]: "+e);
             }
             }
            
            


            2. W.r.t rich:fileUpload demo, we need to define our own entity (File.java) with the members same as org.richfaces.model.UploadItem.java.

            a. Why was it designed (UploadItem.java), such a way? Can we extend it, so that we can fetch both byte[] and java.io.File format? And wire it with rich:fileUpload?

            b. I want to use the collection of UploadItem (List) however I cannot use its methods in UI/.xhtml. coz of absence of setter methods? Please assist.

            Regards,


            • 3. Re: Why? FileUpload: uploadItem.getData() is null !

              2. a.

              'createTempFiles' param defines if transmitted data will be stored to the temp file or will kept in memory during request parsing. If param = TRUE UploadItem contains reference on temp file created, otherwise - array of bytes. If you want to convert data (get byte array from the file or store array to the file) you should implement this logic yourselt. This is not a component's work.

              2. b If you said about collection in UploadEvent this is wrong way to hold UploadItems there, because event is alive only during request. It was designed for purpose when several files uploaded (by form submit).
              Anyway there is uploadData attribute in fileupload. It should be bind to the list. FileUpload will put UploadItem there after each submit occurred.