01.FileUpload

lib

web.xml
<servlet>
    <servlet-name>PrimeFaces Resource Servlet</servlet-name>
    <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>PrimeFaces Resource Servlet</servlet-name>
    <url-pattern>/primefaces_resource/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>com.sun.faces.allowTextChildren</param-name>
    <param-value>true</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>102400</param-value>
</init-param>
<init-param>
    <param-name>uploadDirectory</param-name>
    <param-value>./resources/videos</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.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>


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

<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}" allowTypes="*.jpg;*.png;*.gif;" description="Images"/> 

ManagedBean - helloworld
public class FileUploadController { 
    public void handleFileUpload(FileUploadEvent event) {
            FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }
}  






ManagedBean - more action
public void handleFileUpload(FileUploadEvent event) {
    // get the file from client
    UploadedFile file = event.getFile();


    // rename the file
    String originfilename = file.getFileName();
    String filename = new DateTime().toString("yyyy-MM-dd-HH-mm-ss") + "." + FileHelper.getFileType(originfilename);


    // save the file to server filesystem
    String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/videos");
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(path + File.separatorChar + filename);
        fileOutputStream.write(file.getContents());
    } catch (IOException ex) {
        Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
        return;
    } finally {
        try {
            fileOutputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    // save the file information
    FileUpload fileUpload = new FileUpload();
    fileUpload.setOriginfilename(originfilename);
    fileUpload.setFilename(filename);

    fileUploadJpaController.create(fileUpload);


    // return the message
    FacesMessage msg = new FacesMessage("Succesful", originfilename + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

Comments