0

I am trying to save the image file but the call is not going to the fileHandler method. We have already added filter to the web.xml as mentioned in the primefaces document. This demo I am trying. I also have commons-io and commons-fileupload. How can I correct it?

XHTML page

<h:form enctype="multipart/form-data">
<p:fileUpload
fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced" update="messages" label="Choose a file"
auto="true" sizeLimit="10485760"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
invalidSizeMessage="The maximum file size allowed is 10 Megabytes !"
invalidFileMessage="You are allowed to upload only images !" />
<p:growl id="messages" showDetail="true" life="5000" />
</h:form>

Bean

@ManagedBean(name = "topicUploadBean")
@RequestScoped
public class TopicUploadBean {

    //Primitives
    private static final int BUFFER_SIZE = 6124;


    /** Create a new instance of UploadBean */
    public TopicUploadBean(){
        System.out.println("created FileUploadController instance");

    }

    public void handleFileUpload(FileUploadEvent event){
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();

        File result = new File(extContext.getRealPath("//WEB-INF//files//"+event.getFile().getFileName()));
        System.out.println(extContext.getRealPath("//WEB-INF//files//"+event.getFile().getFileName()));
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(result);
            byte[] buffer = new byte[BUFFER_SIZE];
            int bulk;
            InputStream inputStream = event.getFile().getInputstream();
            while(true){
                bulk = inputStream.read(buffer);
                if (bulk < 0){
                    break;
                }
                fileOutputStream.write(buffer,0,bulk);
                fileOutputStream.flush();
            }
            fileOutputStream.close();
            inputStream.close();
            FacesMessage msg = new FacesMessage("File Description", "file name: " + event.getFile().getFileName() +" File size: "+ event.getFile().getSize()/1024 +" Kb Content type: "+ event.getFile().getContentType() + " the file was uploaded.");
        } catch(IOException e){
            e.printStackTrace();
            FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, " the files were not uploaded!", "");
            FacesContext.getCurrentInstance().addMessage(null, error);
        }
    }

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee" 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-app_3_0.xsd">
    <display-name>emyed-web</display-name>
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>1</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.SEPARATOR_CHAR</param-name>
        <param-value>_</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>#{userPreferences.theme}</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.allowTextChildren</param-name>
        <param-value>true</param-value>
    </context-param>
    <!-- Custom filter to check for user login -->
    <filter>
        <filter-name>EmyEdAccessFilter</filter-name>
        <filter-class>com.zreflect.emyed.filter.EmyEdAccessFilter</filter-class>
    </filter>
    <!-- Filter for uploading files -->
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>10240</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <filter-mapping>
        <filter-name>EmyEdAccessFilter</filter-name>
        <url-pattern>/pages/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--servlet> <servlet-name>Resource Servlet</servlet-name> <servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> </servlet -->
    <servlet>
        <servlet-name>ImageRenderServlet</servlet-name>
        <servlet-class>com.zreflect.emyed.servlets.ImageRenderServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>DownloadAttachmentServlet</servlet-name>
        <servlet-class>com.zreflect.emyed.servlets.DownloadAttachmentServlet</servlet-class>
    </servlet>

    <!-- Atmos newly added -->
    <servlet>
        <description>MeteorServlet</description>
        <servlet-name>MeteorServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class>
        <init-param>
            <param-name>org.atmosphere.servlet</param-name>
            <param-value>com.zreflect.emyed.managedbean.circle.MeteorAtmosphereSharePostServlet</param-value>
        </init-param>
        <init-param>
            <param-name>org.atmosphere.useNative</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>MeteorServlet</servlet-name>
        <url-pattern>/meteor</url-pattern>
    </servlet-mapping>



    <!-- Atmos done -->

    <!--servlet> <servlet-name>Resource Servlet</servlet-name> <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Resource 
        Servlet</servlet-name> <url-pattern>/primefaces_resource/*</url-pattern> 
        </servlet-mapping -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <!--url-pattern>*.iface</url-pattern -->
        <url-pattern>*.jspx</url-pattern>
        <url-pattern>/xmlhttp/*</url-pattern>
        <url-pattern>*.faces</url-pattern>
        <url-pattern>*.jsf</url-pattern>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ImageRenderServlet</servlet-name>
        <url-pattern>/photo/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>DownloadAttachmentServlet</servlet-name>
        <url-pattern>/download/attachment/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>500</error-code>
        <location>/errors/500.xhtml</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/errors/500.xhtml</location>
    </error-page>
    <!-- error-page> <exception-type>javax.faces.application.ViewExpiredException</exception-type> 
        <location>/errors/viewExpired.xhtml</location> </error-page -->
</web-app>
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1595858
  • 3,700
  • 15
  • 66
  • 109
  • do you have commons-fileupload and commons-io jars in your lib folder ? http://stackoverflow.com/a/11951973/617373 – Daniel Sep 12 '12 at 07:34
  • @Daniel i do have commons-fileupload and now i added commons-io. But still it is not going to backend. – user1595858 Sep 12 '12 at 10:28
  • do you have `enctype="multipart/form-data"` in your form and did you add the servlet mapping to web.xml ? – Daniel Sep 12 '12 at 10:29
  • Please check if this answer doesn't already answer your concrete problem: http://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked/8880083#8880083 – BalusC Sep 12 '12 at 11:48
  • @BalusC I have no issue when i create a new project and run it. But in our existing project it is failing. I have pasted my web.xml, let me know if you find any conflicts. – user1595858 Sep 13 '12 at 02:37
  • Is `EmyEdAccessFilter` also executed on file upload requests? If so, is that filter doing its job correctly on those requests? – BalusC Sep 13 '12 at 02:48
  • @BalusC i belive so the filter executes to make sure the session is valid. So what i need to do to make it work? – user1595858 Sep 13 '12 at 03:24
  • Uh, have you debugged the filter or not? Have you confirmed that the filter is blocking the file upload request or not? – BalusC Sep 13 '12 at 03:27
  • @BalusC I just tested it, when i upload the pic, it goes to doFilter method of EmyEdAccessFilter. – user1595858 Sep 13 '12 at 03:58
  • @BalusC I tried again after placing an ID to the component, now it is working. I am not sure how adding ID resolves this problem. – user1595858 Sep 13 '12 at 05:08

1 Answers1

0

Because of your managed bean is "topicUploadBean", not "fileUploadController". Please check your beans, or also post your fileUploadController bean if you have.

oko
  • 1,295
  • 1
  • 14
  • 30