0 Replies Latest reply: May 3, 2012 12:44 PM by Maurizio Gullo RSS

    <s:fileUpload> NullPointerException inputStream jboss as 5.1 and seam 2.2.2

    Maurizio Gullo Newbie

      i've a problem with <s:fileUpload>. i'm using jboss as 5.1 and seam 2.2.2. i hope you have any suggestions. i want to save the inputstream into my file system. the problem is that inputstream (byte[]) is null when i click the upload button

       

       

      my xhtml is:

       

       

      ...
      <h:form enctype="multipart/form-data">
                                    <fieldset>
                                    <s:fileUpload id="filePdf"
                                                        rendered="#{entity.pathStringValue == null}"
                                                        data="#{pdfUploadBean.bytesPdfUploaded}"
                                     />
                                    <h:commandButton id="pdfUploadButton" action="#{pdfUploadBean.savePdfUploaded()}" value="Upload" />
                                    </fieldset>
      </h:form>
      ...
      

       

      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"
                id="WebApp_ID" version="2.5">
                <display-name>carepedia</display-name>
                <welcome-file-list>
                          <welcome-file>index.html</welcome-file>
                </welcome-file-list>
      
                <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>*.seam</url-pattern>
                </servlet-mapping>
      
                <context-param>
                          <param-name>org.jboss.jbossfaces.JSF_CONFIG_NAME</param-name>
                          <param-value>Mojarra-1.2</param-value>
                </context-param>
                <context-param>
                          <param-name>org.richfaces.SKIN</param-name>
                          <param-value>blueSky</param-value>
                </context-param>
                <context-param>
                          <param-name>facelets.LIBRARIES</param-name>
                          <param-value>/taglib/taglib.xml</param-value>
                </context-param>
      
      
                <listener>
                          <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
                </listener>
      
                <filter>
                          <display-name>Ajax4jsf Filter</display-name>
                          <filter-name>ajax4jsf</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>
                <filter-mapping>
                          <filter-name>ajax4jsf</filter-name>
                          <url-pattern>/*</url-pattern>
                </filter-mapping> 
      
                <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>
                          <init-param>
                               <param-name>maxRequestSize</param-name>
                               <param-value>10000000</param-value>
                          </init-param>
                </filter>
                <filter-mapping>
                          <filter-name>Seam Filter</filter-name>
                          <url-pattern>/*</url-pattern>
                </filter-mapping> 
      
                <servlet>
                          <servlet-name>Seam Resource Servlet</servlet-name>
                          <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
                </servlet>
                <servlet-mapping>
                          <servlet-name>Seam Resource Servlet</servlet-name>
                          <url-pattern>/seam/resource/*</url-pattern>
                </servlet-mapping>
                <context-param>
                          <param-name>facelets.DEVELOPMENT</param-name>
                          <param-value>true</param-value>
                </context-param>
                <context-param>
                          <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
                          <param-value>.xhtml</param-value>
                </context-param>
      
                <security-constraint>
                          <display-name>Restrict raw XHTML Documents</display-name>
                          <web-resource-collection>
                                    <web-resource-name>XHTML</web-resource-name>
                                    <url-pattern>*.xhtml</url-pattern>
                          </web-resource-collection>
                          <auth-constraint />
                </security-constraint>
      </web-app>
      
      

       

      components.xml

       

       

      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/components"
                  xmlns:core="http://jboss.com/products/seam/core"
                  xmlns:persistence="http://jboss.com/products/seam/persistence"
                  xmlns:drools="http://jboss.com/products/seam/drools"
                  xmlns:bpm="http://jboss.com/products/seam/bpm"
                  xmlns:security="http://jboss.com/products/seam/security"
                  xmlns:mail="http://jboss.com/products/seam/mail"
                  xmlns:web="http://jboss.com/products/seam/web"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation=
                      "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                       http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
                       http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
                       http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
                       http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
                       http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                       http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
                       http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">
      
      
         <core:init debug="true" jndi-pattern="@jndiPattern@"/>
      
      
         <core:manager concurrent-request-timeout="500"
                       conversation-timeout="120000"
                       conversation-id-parameter="cid"
                       parent-conversation-id-parameter="pid"/>
      
      
         <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
         <web:hot-deploy-filter url-pattern="*.seam"/>
      
         <persistence:managed-persistence-context name="carepediaEntityManager" auto-create="true"
                                 persistence-unit-jndi-name="java:/carepediaEntityManagerFactory"/>
      
      
         <drools:rule-base name="securityRules">
            <drools:rule-files><value>/security.drl</value></drools:rule-files>
         </drools:rule-base>
      
      
         <security:rule-based-permission-resolver security-rules="#{securityRules}"/>
      
      
         <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
      
      
         <event type="org.jboss.seam.security.notLoggedIn">
            <action execute="#{redirect.captureCurrentView}"/>
         </event>
         <event type="org.jboss.seam.security.loginSuccessful">
            <action execute="#{redirect.returnToCapturedView}"/>
         </event>
      
                <web:multipart-filter create-temp-files="true" max-request-size="1000000" url-pattern="*.seam"/>
      
                <mail:mail-session host="localhost" port="25"/>
      
      </components>
      
      

       

      PdfUploadBean.java

       

       

      package org.domain.carepedia.session;
      
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      
      
      import java.io.File;
      import java.io.FileOutputStream;
      import java.util.UUID;
      
      
      @Name ("pdfUploadBean")
      @Scope (ScopeType.PAGE) 
      
      
      public class PdfUploadBean{
      
                private byte[] bytesPdfUploaded;
                private String path;
      
                public PdfUploadBean() {
                    this.path = "C:\\Users\\Maurizio\\workspace\\git\\carepedia\\carepedia\\WebContent\\pdf\\" + UUID.randomUUID().toString() + ".pdf";
          }
      
          public byte[] getBytesPdfUploaded() {
                          return bytesPdfUploaded;
                }
      
      
                public void setBytesPdfUploaded(byte[] bytesPdfUploaded) {
                          this.bytesPdfUploaded = bytesPdfUploaded;
                }
      
      
                public String getPath() {
                          return path;
                }
      
      
                public void setPath(String path) {
                          this.path = path;
                }
      
          public void savePdfUploaded(){
                    File file = new File(path);
                    try {
                              FileOutputStream fos = new FileOutputStream(file);
                                    fos.write(bytesPdfUploaded);
                                    fos.flush();
                                    fos.close();
                          } catch (Exception e) {
                                    e.printStackTrace();
                          }
      
          } 
      
      }