Problem in uploading file

UI Components for JSF
Post Reply
Basit
Posts: 14
Joined: 03 Oct 2011, 07:33

28 Oct 2011, 11:45

Hi to all,
Hope you all will be fine. I want to upload file in a database, i.e, user select file from the computer and when it click on the save button then the file should save in a database.
I am trying Primefaces file upload but although the message is that file has been uploaded but file not showing in a directory. Here is my code. Please tell me why the file is not showing in a directory.

Code: Select all

ManagedBean
@RequestScoped
public class UploadBean {

    private static final int BUFFER_SIZE = 6124345;

    /** Creates a new instance of UploadBean */
    public UploadBean() {

    } //end of constructor

    public void handleFileUpload(FileUploadEvent event) {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();

        File result = new File(externalContext.getRealPath("//WEB-INF//upload") + "//" + event.getFile().getFileName() );

        try {

            FileOutputStream fileOutputStream = new FileOutputStream(result);

            byte[] buffer = new byte[BUFFER_SIZE];

            int bulk;

            // Here you get uploaded picture bytes while debugging you can see that 34818
            InputStream inputStream = event.getFile().getInputstream();

            while(true) {

                bulk = inputStream.read(buffer);

                if (bulk < 0) {

                    break;

                } //end of if

                fileOutputStream.write(buffer, 0, bulk);
                fileOutputStream.flush();

            } //end fo while(true)

            fileOutputStream.close();
            inputStream.close();

            FacesMessage msg = new FacesMessage("Succesful",event.getFile().getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);

        }  catch (IOException e) {

            e.printStackTrace();

            FacesMessage error = new FacesMessage("The files were not uploaded!");
            FacesContext.getCurrentInstance().addMessage(null, error);

        }

    } //end of handleFileUpload()

} // end of class UploadBean


Code: Select all


<h:head>
        <title>File upload</title>
    </h:head>

    <h:body>

        <h:form id="myForm"
                enctype="multipart/form-data"
                prependId="false" >

            <p:growl id="messages"  showSummary="true" showDetail="true" />

            <p:fileUpload id="upload"
                          fileUploadListener="#{uploadBean.handleFileUpload}"
                          update="messages"
                          sizeLimit="100000"
                          multiple="true"
                          label="choose"
                          allowTypes="*.png;*.jpg;*.gif;"
                          description="Images" />


        </h:form>


        
    </h:body>
</html>

Code: Select all

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

<web-app 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">


    <context-param>
        
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    
    </context-param>
    
    
    <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>781312</param-value>
        </init-param>
        
    </filter>
   
    <filter-mapping>
        
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        
    </filter-mapping>


    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

after message display that file has been uploaded , when i checked the direcctor WEB_INF/upload/ then no file present there. Why it is saying that file has been uploaded. Where can i get the uploaded file?

Thanks

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 47 guests