1 Reply Latest reply on Nov 9, 2011 12:53 PM by Andrew Cheung

    RichFaces3.3.3Final: FileUpload question

    Andrew Cheung Newbie

      Hi. I'm trying to get the FileUpload example (with slight modifications) working in my environment, but I can't get it working properly. When I clicked on "Add" and then choose a gif file (small 1kb file) and click "Upload", the progress bar appeared, then in a second, it says "Done". However, the file doesn't show on the right hand side.

       

      My environment is as follows:

      JBoss EPP 5.1.1

      JSF 1.2 (this is the most up-to-date version of JSF that GateInPortal 3.1.0 supports)

      JBoss Portlet Bridge 2.0.0

      RichFaces 3.3.3Final

       

      Any help is appreciated. Thanks.

       

        -Andrew

       

      -----------------------------------

      The code is as follows:

       

      FileUpload.xhtml:


      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml"

            xmlns:ui="http://java.sun.com/jsf/facelets"

            xmlns:h="http://java.sun.com/jsf/html"

            xmlns:f="http://java.sun.com/jsf/core"

            xmlns:c="http://java.sun.com/jstl/core"

            xmlns:a4j="http://richfaces.org/a4j"

            xmlns:rich="http://richfaces.org/rich">

            <ui:composition template="/templates/common.xhtml">

               <style>

      .top {

          vertical-align: top;

         

      }

      .info {

          height: 202px;

          overflow: auto;

      }

      </style>

          <ui:define name="body">

          <h:form id="MyFileUploadForm" enctype="multipart/form-data">

              <h:panelGrid columns="2" columnClasses="top,top">

                  <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"

                      maxFilesQuantity="#{fileUploadBean.uploadsAvailable}"

                      id="upload"

                      immediateUpload="#{fileUploadBean.autoUpload}"

                      acceptedTypes="jpg, gif, png, bmp" allowFlash="#{fileUploadBean.useFlash}" uploadData="#{file.data}">

                      <a4j:support event="onuploadcomplete" reRender="info" />

                  </rich:fileUpload>

                  <h:panelGroup id="info">

                  <outputText value="#{file.name}"></outputText>

                      <rich:panel bodyClass="info2">

                          <f:facet name="header">

                              <h:outputText value="Uploaded Files Info" />

                          </f:facet>

                          <h:outputText value="No files currently uploaded"

                              rendered="#{fileUploadBean.size==0}" />

                          <rich:dataGrid columns="1" value="#{fileUploadBean.files}"

                              var="file" rowKeyVar="row">

                              <rich:panel bodyClass="rich-laguna-panel-no-header">

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

                              </rich:panel>

                          </rich:dataGrid>

                      </rich:panel>

                      <rich:spacer height="3"/>

                      <br />

                      <a4j:commandButton action="#{fileUploadBean.clearUploadData}"

                          reRender="info, upload" value="Clear Uploaded Data"

                          rendered="#{fileUploadBean.size>0}" />

                  </h:panelGroup>

              </h:panelGrid>

          </h:form>

              </ui:define>

          </ui:composition>

            </html>

       

      --------------------

      FileUploadBean.java :

      package demo;

      import java.io.IOException;

      import java.io.OutputStream;

      import java.util.ArrayList;

       

      import org.richfaces.event.UploadEvent;

      import org.richfaces.model.UploadItem;

       

      public class FileUploadBean{

       

         

          private ArrayList<File> files = new ArrayList<File>();

          private int uploadsAvailable = 1;

          private boolean autoUpload = false;

          private boolean useFlash = false;

          public int getSize() {

              if (getFiles().size()>0){

                  return getFiles().size();

              }else

              {

                  return 0;

              }

          }

       

          public FileUploadBean() {

       

          }

       

          public void paint(OutputStream stream, Object object) throws IOException {

       

              stream.write(getFiles().get((Integer)object).getData());

          }

          public void listener(UploadEvent event) throws Exception{

       

             UploadItem item = event.getUploadItem();

              File file = new File();

              file.setLength(item.getData().length);

              file.setName(item.getFileName());

              file.setData(item.getData());

              files.add(file);

             

              uploadsAvailable--;

          } 

           

          public String clearUploadData() {

              files.clear();

              setUploadsAvailable(5);

              return null;

          }

         

          public long getTimeStamp(){

              return System.currentTimeMillis();

          }

         

          public ArrayList<File> getFiles() {

              return files;

          }

       

          public void setFiles(ArrayList<File> files) {

              this.files = files;

          }

       

          public int getUploadsAvailable() {

              return uploadsAvailable;

          }

       

          public void setUploadsAvailable(int uploadsAvailable) {

              this.uploadsAvailable = uploadsAvailable;

          }

       

          public boolean isAutoUpload() {

              return autoUpload;

          }

       

          public void setAutoUpload(boolean autoUpload) {

              this.autoUpload = autoUpload;

          }

       

          public boolean isUseFlash() {

              return useFlash;

          }

       

          public void setUseFlash(boolean useFlash) {

              this.useFlash = useFlash;

          }

       

      }

       

      --------------

      File.java :

       

      package demo;

       

       

      public class File {

       

         

          private String Name;

          private String mime;

          private long length;

          private byte[] data;

          public byte[] getData() {

              return data;

          }

          public void setData(byte[] data) {

              this.data = data;

        

          }

          public String getName() {

              return Name;

          }

          public void setName(String name) {

           

              Name = name;

              int extDot = name.lastIndexOf('.');

              if(extDot > 0){

                  String extension = name.substring(extDot +1);

                  if("bmp".equals(extension)){

                      mime="image/bmp";

                  } else if("jpg".equals(extension)){

                      mime="image/jpeg";

                  } else if("gif".equals(extension)){

                      mime="image/gif";

                  } else if("png".equals(extension)){

                      mime="image/png";

                  } else {

                      mime = "image/unknown";

                  }

              }

          }

          public long getLength() {

              return length;

          }

          public void setLength(long length) {

              this.length = length;

          }

         

          public String getMime(){

              return mime;

          }

      }

       

       

      ----------------------

      web.xml :

       

      <?xml version="1.0" encoding="UTF-8"?>

      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

        <description>Facelets StarterKit</description>

        <display-name>MyPrototype</display-name>

      <context-param>

        <param-name>javax.faces.CONFIG_FILES</param-name>

        <param-value>/WEB-INF/faces-config.xml</param-value>

      </context-param>

        <context-param>

          <param-name>com.sun.faces.validateXml</param-name>

          <param-value>true</param-value>

        </context-param>

        <context-param>

          <param-name>com.sun.faces.verifyObjects</param-name>

          <param-value>true</param-value>

        </context-param>

        <context-param>

          <param-name>facelets.DEVELOPMENT</param-name>

          <param-value>true</param-value>

        </context-param>

        <context-param>

          <param-name>facelets.REFRESH_PERIOD</param-name>

          <param-value>2</param-value>

        </context-param>

        <context-param>

          <param-name>javax.faces.DEFAULT_SUFFIX</param-name>

          <param-value>.xhtml</param-value>

        </context-param>

        <context-param>

          <param-name>javax.faces.STATE_SAVING_METHOD</param-name>

          <param-value>client</param-value>

        </context-param>

         <filter>

        <display-name>Ajax4jsf Filter</display-name>

        <filter-name>richfaces</filter-name>

        <filter-class>org.ajax4jsf.Filter</filter-class>

        <init-param>

        <param-name>createTempFiles</param-name>

        <param-value>false</param-value>

        </init-param>

        <init-param>

        <param-name>maxRequestSize</param-name>

        <param-value>20000000</param-value>

        </init-param>

         </filter>

        <servlet>

          <servlet-name>Faces Servlet</servlet-name>

          <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

          <load-on-startup>1</load-on-startup>

        </servlet>

        <servlet-mapping>

          <servlet-name>Faces Servlet</servlet-name>

          <url-pattern>*.jsf</url-pattern>

          <url-pattern>/faces/*</url-pattern>

        </servlet-mapping>

         <filter-mapping>

        <filter-name>richfaces</filter-name>

        <servlet-name>Faces Servlet</servlet-name>

        <dispatcher>REQUEST</dispatcher>

        <dispatcher>FORWARD</dispatcher>

        <dispatcher>INCLUDE</dispatcher>

        <dispatcher>ERROR</dispatcher>

      </filter-mapping>

      </web-app>

       

      ---------------

      faces-config.xml :

       

       

      <?xml version="1.0" encoding="UTF-8"?>

      <faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"

      xmlns:xi="http://www.w3.org/2001/XInclude"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

      <managed-bean>

        <description>FileUploadBean</description>

        <managed-bean-name>fileUploadBean</managed-bean-name>

        <managed-bean-class>demo.FileUploadBean</managed-bean-class>

        <managed-bean-scope>session</managed-bean-scope>

      </managed-bean>

      <managed-bean>

        <description>File</description>

        <managed-bean-name>file</managed-bean-name>

        <managed-bean-class>demo.File</managed-bean-class>

        <managed-bean-scope>session</managed-bean-scope>

      </managed-bean>

      <application>

        <view-handler>org.jboss.portletbridge.application.PortletViewHandler</view-handler>

        <state-manager>org.jboss.portletbridge.application.PortletStateManager</state-manager>

        <locale-config>

         <default-locale>en</default-locale>

         <supported-locale>en</supported-locale>

         <supported-locale>en_US</supported-locale>

        </locale-config>

        <resource-bundle>

         <base-name>labels</base-name>

         <var>rslabels</var>

        </resource-bundle>

      </application>

      </faces-config>

       

      ------------------

      portlet.xml :

       

       

      <?xml version="1.0" encoding="UTF-8"?>

      <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"

      version="2.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd

         http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">

         <portlet>

            <portlet-name>MyPrototypePortlet</portlet-name>

            <portlet-class>javax.portlet.faces.GenericFacesPortlet</portlet-class>

            <supports>

               <mime-type>text/html</mime-type>

               <portlet-mode>view</portlet-mode>

               <portlet-mode>edit</portlet-mode>

               <portlet-mode>help</portlet-mode>

            </supports>

            <portlet-info>

               <title>MyPrototype Portlet</title>

            </portlet-info>

            <init-param>

               <name>javax.portlet.faces.defaultViewId.view</name>

               <value>/pages/FileUpload.xhtml</value>

            </init-param>

            <init-param>

               <name>javax.portlet.faces.defaultViewId.edit</name>

               <value>/pages/FileUpload.xhtml</value>

            </init-param>

       

            <init-param>

               <name>javax.portlet.faces.defaultViewId.help</name>

               <value>/pages/FileUpload.xhtml</value>

            </init-param>

       

         </portlet>

      </portlet-app>