JBoss application server tutorials

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Primefaces file upload example


primefaces file upload exampleThe PrimeFaces File upload component can be used to perform server side file uploads. Let's see in this quick tutorial how to achieve it in some simple steps.

The first thing you need is adding some libraries to your application. As a matter of fact, PrimeFaces file upload relies on Apache commons-file-upload and commons-io libraries. So dowload them and add them to your WEB-INF/lib path:

Here you can download Apache Commons io

Here you can download Apache Commons File Upload


As far as web.xml configuration is concerned you need to declare the PrimeFaces filter in order to enable file uploading

  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <servlet-name>Faces Servlet</servlet-name>

Good, your configuration is completed. Let's see a concrete example:

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


     <h:form id="form" >

        <p:fileUpload fileUploadListener="#{fileUploadController.upload}"
6.allowTypes="/(\.|\/)(gif|jpe?g|png)$/" sizeLimit="100000" description="Select Images"/>


The tag does the job of sending the multipart stream to the server. In the bean declared (fileUploadController) a listener will fire which will let you handle the file and do what you want with it.

Here's the bean implementation:

package com.sample.bean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
public class FileUploadController {
   private String destination="D:\\tmp\\";

    public void upload(FileUploadEvent event) {  
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        // Do what you want with the file        
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {


    public void copyFile(String fileName, InputStream in) {
           try {
                // write the inputStream to a FileOutputStream
                OutputStream out = new FileOutputStream(new File(destination + fileName));
                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                System.out.println("New file created!");
                } catch (IOException e) {

The upload method provides as parameter the FileUploadEvent from which you can retrieve the temporary file uploaded on the server. Once you have got it, use the stream to copy it or add it to a relational database.

primefaces file upload example

As additional note, you can choose the path where the file is uploaded on the server by setting some additional parameters like uploadDirectory and thresholdSize

thresholdSize specifies the maximum file size in bytes to keep uploaded files in memory. If it
exceeds this limit, it’ll be temporarily written to disk.
uploadDirectory is the folder where to keep temporary files that exceed thresholdSize.

  <filter-name>PrimeFaces FileUpload Filter</filter-name>

You should however not rely on these attributes to define a stable storage of uploaded file. Always use IO Streams to collect the file and then store them in your favourite storage location<

Primefaces file upload example
Francesco Google+
Top Programming Sites
You are here Home