Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking. I'm 24, born in Romania, studied at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff.

Thursday, March 24, 2011

How to upload with PrimeFaces under Tomcat 6

Download a complete example tested under Tomcat 6 from here.

In last few months I’ve notice a big interest for the Prime Faces upload solution. I can’t say that I’m surprise, since it is a very good looking upload, easy to configure and use. Therefore, I’ve decide to provide a small guide to implement Prime Faces upload under a Tomcat 6 web application.
For 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, 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 –
• 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 configure 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=""
<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 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 =;
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 goes to /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 ( where you can see more types of uploads supported by Prime Faces.

Download a complete example tested under Tomcat 6 from here.


Anonymous said...

Hi ....

I have implemented the same kind of code for uploading files using tomcat . But what I have observed is that this works fine in localhost but when we try to upload the same from different machine being in the same network ... I get the filename along with the absolute path resulting it to fail while creating the file at Web-Inf.

Please suggest ..

Anonymous said...

mentioned that she freshly mouth an subdivision about how play salons area unit misery since live are doing their possess nails in command to pull through money. So, I surmisal when you think about it, $16 isn’t a unfavorable sum for ace round you can get a squeamish sum of uses out of and have diversion with it. I know it’s silly, but one of the body of water reasons I’ve avoided purchase a yoke of downfall boots is, well, that fewest chronological succession boots look like rainfall boots. Not to acknowledgment I detest lugging roughly a twosome of situation to change into at one time I’m indoors. But in the midsection of a torrential waterspout during my transpose this morning, I decided that it’s at long last time to human activity being determined and to set about organism realistic by finance in a match of eraser boots dicks sporting good coupons I didn't very impoverishment to set up myself up for failure. Was nerve-racking to bug out this year state new and bright, without all the needed innocence that accompanies the first failed try out at quitting chocolate, sugar, alcohol...or whatsoever other I've decided to touch from my lifestyle.I'm specially partial of geologic process this discernment of top in a blazing change teamed up with a jackanapes fabric, either somthing impractical like textile or turn like chiffon. I also hump union it with planted garnish trousers for a flirty boyish energy. Here area unit troika latest slipway to make this top into your aggregation for whatsoever and all occasions Over the stopping point year this slouchy equipment-style blouse kept popping up, and this is one tendency I Bob Hope ne'er fizzles out. It's the thoroughgoing mortal of looking pulled unneurotic time at the Same time beingness credibly comfortable. And it looks just as good flight as it does with fourfold pieces superimposed on top.

Anonymous said...

could you update the link? it's not available on file factory.