Problems with rich:fileUpload
webx Sep 5, 2008 3:04 PMHy, 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".