3 Replies Latest reply on Sep 6, 2008 6:58 PM by Vinicius Dallacqua

    Problems with rich:fileUpload

    Vinicius Dallacqua Newbie

      Hy, i'm having troubles with the rich:fileUpload component. The problem is that the UploadEvent event object is not getting the filedata (getData() method). To simplify the understanding i'll put my codes here.


      <%@page contentType="text/html"%>
      <%@page pageEncoding="UTF-8"%>
      
      <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
      <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
      <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
      <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
      
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
      
      <%--
       This file is an entry point for JavaServer Faces application.
      --%>
      
      <html>
       <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
       </head>
       <body>
       <f:view>
       <style>
      .top {
       vertical-align: top;
      
      }
      .info {
       height: 202px;
       overflow: auto;
      }
      </style>
      
       <h:form>
       <h:panelGrid columns="2" columnClasses="top,top">
       <rich:fileUpload uploadData="#{fileUploadBean.filesUp}" fileUploadListener="#{fileUploadBean.listener}"
       maxFilesQuantity="#{fileUploadBean.uploadsAvailable}"
       id="upload" addControlLabel="Adicionar" cancelEntryControlLabel="Cancelar"
       clearAllControlLabel="Limpar Todos" clearControlLabel="Limpar"
       uploadControlLabel="Enviar" stopEntryControlLabel="Parar"
       stopControlLabel="Parar" doneLabel="Terminado" reRender="table"
       immediateUpload="#{fileUploadBean.autoUpload}"
       acceptedTypes="jpg, gif, png, bmp">
       <f:facet name="label">
       <h:outputText value="{_KB}KB de {KB}KB enviado(s) --- {mm}:{ss}" />
       </f:facet>
       <a4j:support event="onuploadcomplete" reRender="info, log" />
       </rich:fileUpload>
       <h:panelGroup id="info">
       <rich:panel bodyClass="info">
       <f:facet name="header">
       <h:outputText value="Informações dos arquivos enviados" />
       </f:facet>
       <h:outputText value="Sem arquivos enviados até o momento"
       rendered="#{fileUploadBean.size==0}" />
       <rich:dataGrid columns="1" value="#{fileUploadBean.files}"
       var="file" rowKeyVar="row">
       <rich:panel bodyClass="rich-laguna-panel-no-header">
       <h:panelGrid columns="2">
       <a4j:mediaOutput element="img" mimeType="#{file.mime}"
       createContent="#{fileUploadBean.paint}" value="#{row}"
       style="width:100px; height:100px;" cacheable="false">
       <f:param value="#{fileUploadBean.timeStamp}" name="time"/>
       </a4j:mediaOutput>
       <h:panelGrid columns="2">
       <h:outputText value="Nome do Arquivo:" />
       <h:outputText value="#{file.name}" />
       <h:outputText value="Tamanho do arquivo(bytes):" />
       <h:outputText value="#{file.length}" />
       </h:panelGrid>
       </h:panelGrid>
       </rich:panel>
       </rich:dataGrid>
       </rich:panel>
       <rich:spacer height="3"/>
       <br />
       <a4j:commandButton action="#{fileUploadBean.clearUploadData}"
       reRender="info, upload" value="Limpar dados enviados"
       rendered="#{fileUploadBean.size>0}" />
       </h:panelGroup>
       <br /><br /><h:outputText id="log" value="#{fileUploadBean.log}" />
       </h:panelGrid>
       </h:form>
       </f:view>
       </body>
      </html>
      



      public synchronized void listener(UploadEvent event) throws Exception{
       UploadItem item = event.getUploadItem();
       ExternalContext con = FacesContext.getCurrentInstance().getExternalContext();
       ServletContext sCon = (ServletContext) con.getContext();
       TheFile file = new TheFile();
       try{
       File theFile = event.getUploadItem().getFile();
       File creationFile = new File((String) sCon.getRealPath("/")+"upload/", theFile.getName());
      
       if(!creationFile.exists()){
       FileOutputStream fileOutStream = new FileOutputStream(creationFile);
       fileOutStream.write(event.getUploadItem().getData());
       fileOutStream.flush();
       fileOutStream.close();
       }
       file.setLength(theFile.length());
       file.setName(item.getFileName());
       file.setData(item.getData());
       getFiles().add(file);
       uploadsAvailable--;
       } catch(Exception ex){
       setLog("Deu erro: "+ex+" total de arquivos enviados: "+filesUp.size());
       }
       }
      


      The problem is, when my code runs the line:
      fileOutStream.write(event.getUploadItem().getData());

      The systems returns a NullPointerException. The getData() method do is returning null, but the others methods which refers to the uploaded file is working (getting the correct data). What can it be?!

      ps.:i've utilized the live example implementation (http://livedemo.exadel.com/richfaces-demo/richfaces/fileUpload.jsf;jsessionid=9037A2E803B98B2B21D33E7A798A2D9E?c=fileUpload), this is just a test class. I've observed that the on line "reRender="table"" on the rich:fileUpload component at the example apear an error line saying: "Atribute reRender invalid for tag fileUpload acording to TLD".


        • 1. Re: Problems with rich:fileUpload
          Vinicius Dallacqua Newbie

          the problem is now solved, but still the argumentation...why the getData() method don't return the file data?!

          for the ones that want the resolution is this:

          public synchronized void listener(UploadEvent event) throws Exception{
           UploadItem item = event.getUploadItem();
           String fileName = item.getFileName();
           ExternalContext con = FacesContext.getCurrentInstance().getExternalContext();
           ServletContext sCon = (ServletContext) con.getContext();
           String filepath = sCon.getRealPath("/upload/");
           TheFile file = new TheFile();
           try{
           File uploadFile = new File(filepath, fileName);
          
           if(!uploadFile.exists()){
           FileInputStream fis = new FileInputStream(item.getFile());
           FileOutputStream out = new FileOutputStream(uploadFile);
           int bytes = 0;
           byte[] bteFile = new byte[(int)event.getUploadItem().getFile().length()];
           while ((bytes = fis.read(bteFile)) != -1) {
           out.write(bteFile, 0, bytes);
           }
           file.setLength(event.getUploadItem().getFile().length());
           file.setName(item.getFileName());
           file.setData(bteFile);
           getFiles().add(file);
           uploadsAvailable--;
           }
           } catch(Exception ex){
           setLog("Deu erro: "+ex+" total de arquivos enviados: "+filesUp.size());
           }
           }



          • 2. Re: Problems with rich:fileUpload
            Bogumil Zieba Newbie

            Take a look at this:
            http://www.jboss.org/file-access/default/members/jbossrichfaces/freezone/docs/apidoc_framework/org/richfaces/model/UploadItem.html

            Probable solutions of this problem is to change your web.xml to
            <filter-class>org.ajax4jsf.Filter</filter-class>
            <init-param>
            <param-name>createTempFiles</param-name>
            <param-value>false</param-value>
            </init-param>

            INSTEAD

            <filter-class>org.ajax4jsf.Filter</filter-class>
            <init-param>
            <param-name>createTempFiles</param-name>
            <param-value>true</param-value>
            </init-param


            Cheers,
            Bogumil Zieba
            bogumil.zieba[at]gmail.com

            • 3. Re: Problems with rich:fileUpload
              Vinicius Dallacqua Newbie

              Ok, thanks.

              I'll try this solution and after i'll reply here. Just to try the richfaces pattern.