Over a million developers have joined DZone.

How to Upload with PrimeFaces under Tomcat 6

DZone's Guide to

How to Upload with PrimeFaces under Tomcat 6

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

In last few months I’ve noticed a big interest in the Prime Faces upload solution. I can’t say that I’m surprised, since it is a very good looking upload, easy to configure and use. Therefore, I’ve decided to provide a small guide for the  implementation of the Prime Faces upload under a Tomcat 6 web application.
For a start, you will need the following JARs, under your /lib folder (you can take them separately from web, or if you take this complete example from http://www.filefactory.com/file/ca41567/n/prime_faces_upload.rar, then they are already included):

• primeface-2.2.RC2.jar (this is what I’ve used, but you can take the latest Prime Faces library from PrimeFaces site – http://www.primefaces.org).
• jsf-api-2.0.2-FCS.jar (again, take the latest, if you consider that you need it)
• jsf-impl-2.0.2-FCS.jar
• el-api-2.2.jar
• el-impl-2.2.jar
• commons-io-1.4.jar
• commons-fileupload-1.2.jar

Next, I’ve configured the application descriptor, web.xml (depending on Prime Faces version, these configurations may be slightly different). In this descriptor I’ve configured Prime Faces upload filter:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 
<description>Context param for JSTL 1.2 to work in Tomcat 6 sun RI
<description>Parameter required by Mojarra 2.0</description>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<servlet-name>Faces Servlet</servlet-name>
<servlet-name>Faces Servlet</servlet-name>

Next, I wrote a simple JSF page to test the Prime Faces upload component:
<?xml version="1.0" encoding="UTF-8"?>                              

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml"
<h:outputText value="Select the files to upload (maxim 2MB):"/>
<p:growl id="uploadMessages" showSummary="true" showDetail="true"/>
<h:form enctype="multipart/form-data" prependId="false">
<p:fileUpload update="uploadMessages"

As you can see, p:fileUpload is very flexible and customizable, allowing us to specify upload characteristics with a few simple attributes. Going further, we need a managed bean capable of dealing with upload itself. A possible implementation (probably not the best, but for sure not the worst) is listed below:
package com.extensions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.FileUploadEvent;

@ManagedBean(name = "fileUploadController")
public class uploadFilesBean {

private static final int BUFFER_SIZE = 6124;
private String folderToUpload;

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

public void handleFileUpload(FileUploadEvent event) {

ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();

File result = new File(extContext.getRealPath("//WEB-INF//files//" +
//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) {
fileOutputStream.write(buffer, 0, bulk);


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

} catch (IOException e) {

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

The uploaded files go to the /WEB-INF/files folder.

Here it is a nice screenshot of Prime Faces upload:

That was a simple example of Prime Faces upload. Do not hesitate to follow Prime Faces - ShowCase site ( http://www.primefaces.org/showcase/ui/fileUploadHome.jsf) where you can see more types of uploads supported by Prime Faces.

Download a complete example tested under Tomcat 6 from here.


From http://e-blog-java.blogspot.com/2011/03/how-to-upoad-with-primefaces-under.html

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}