Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Upload Files with JSF and MyFaces

Upload Files with JSF and MyFaces

September 10th, 2009 Leave a comment Go to comments

It is common requirement in the web applications to upload files. Even though HTML and browsers support this, JSF core tags don’t provide any components for the file upload.  Fortunately JSF’s other third party components provide such components. There are many implementations available nowadays.  Among them I found the Apache tomahawk library tag helpful. In this tutorial I will explain how you can upload a file using tomahawk library. It explains you how a simple image file can be uploaded, read and saved.

Requirements:   JSF 1.2. and tomahawk library.
If you are new to JSF please go through the JSF Beginner’s Tutorial.

In this tutorial we assume that you have a JSF application running . This tutorial explains you how you can configure tomahawk in the existing JSF application and use fileupload component.

First of all, to use the file upload component, we need to configure the tomahawk library.
Configuration:

Down load tomahawk library from
http://myfaces.apache.org/tomahawk/download.html

Copy the lib/ tomahawk-1.1.9.jar to your webapp/WEB-INF/lib directory.

Also download common dependency jars and copy them to lib.
I have following in my web-inf/lib

commons-logging-api.jar
commons-logging.jar
commons-lang.jar
commons-fileupload-1.2.1.jar
commons-el.jar
commons-digester.jar
commons-collections-3.2.jar
commons-codec.jar
commons-beanutils.jar
commons-validator.jar
commons-lang-2.3.jar
commons-io-1.1.jar
commons-discovery.jar
commons-attributes.jar
commons-httpclient-3.0-rc3.jar

By adding the tomhawk jar to lib, we can use the myfaces components in our jsf pages. The myfaces components will need additional support like scripts, stylesheets etc. They will be applied by the MyfacesExtensionFilter while rendering. This Filter needs to be configured in the web.xml file. Also the myfaces need some context parameters whose default values can be changed by defining them in web.xml file.

Edit your web.xml file to include following in appropriate places:

[code]]czoyMTpcIiZsdDtjb250ZXh0LXBhcmFtJmd0O1wiO3tbJiomXX0=[[/code]

[code]]czoxOlwiIFwiO3tbJiomXX0=[[/code][code]]czo2OTpcIiAmbHQ7cGFyYW0tbmFtZSZndDtqYXZheC5mYWNlcy5TVEFURV9TQVZJTkdfTUVUSE9EJmx0Oy9wYXJhbS1uYW1lJmd0O1wie1smKiZdfTt7WyYqJl19[[/code]

[code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pcIiAmbHQ7aW5pdC1wYXJhbSZndDtcIjt7WyYqJl19[[/code]

[code]]czo0OTpcIiAmbHQ7cGFyYW0tbmFtZSZndDttYXhGaWxlU2l6ZSZsdDsvcGFyYW0tbmFtZSZndDtcIjt7WyYqJl19[[/code]

[code]]czo0MzpcIiAmbHQ7cGFyYW0tdmFsdWUmZ3Q7MjBtJmx0Oy9wYXJhbS12YWx1ZSZndDtcIjt7WyYqJl19[[/code]

[code]]czo1ODpcIiAmbHQ7ZGVzY3JpcHRpb24mZ3Q7U2V0IHRoZSBzaXplIGxpbWl0IGZvciB1cGxvYWRlZCBmaWxlcy5cIjt7WyYqJl19[[/code]

[code]]czoyMjpcIiBGb3JtYXQ6IDEwIC0gMTAgYnl0ZXNcIjt7WyYqJl19[[/code]

[code]]czoxMjpcIiAxMGsgLSAxMCBLQlwiO3tbJiomXX0=[[/code]

[code]]czoxMjpcIiAxMG0gLSAxMCBNQlwiO3tbJiomXX0=[[/code]

[code]]czoxMDpcIiAxZyAtIDEgR0JcIjt7WyYqJl19[[/code]

[code]]czoyMTpcIiAmbHQ7L2Rlc2NyaXB0aW9uJmd0O1wiO3tbJiomXX0=[[/code]

[code]]czoyMDpcIiAmbHQ7L2luaXQtcGFyYW0mZ3Q7XCI7e1smKiZdfQ==[[/code]

[code]]czoxNjpcIiAmbHQ7L2ZpbHRlciZndDtcIjt7WyYqJl19[[/code]

[code]]czoxOlwiIFwiO3tbJiomXX0=[[/code][code]]czoyMzpcIiAmbHQ7ZmlsdGVyLW1hcHBpbmcmZ3Q7XCI7e1smKiZdfQ==[[/code]

[code]]czo2MzpcIiAmbHQ7ZmlsdGVyLW5hbWUmZ3Q7TXlGYWNlc0V4dGVuc2lvbnNGaWx0ZXImbHQ7L2ZpbHRlci1uYW1lJmd0O1wiO3tbJiomXX0=[[/code]

[code]]czo5NDpcIiAmbHQ7IS0tIHNlcnZsZXQtbmFtZSBtdXN0IG1hdGNoIHRoZSBuYW1lIG9mIHlvdXIgamF2YX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2lvbnNGaWx0ZXImbHQ7L2ZpbHRlci1uYW1lJmd0O1wiO3tbJiomXX0=[[/code]

[code]]czo0NzpcIiAmbHQ7dXJsLXBhdHRlcm4mZ3Q7Ki5mYWNlcyZsdDsvdXJsLXBhdHRlcm4mZ3Q7XCI7e1smKiZdfQ==[[/code]

[code]]czoyNTpcIiAmbHQ7L2ZpbHRlci1tYXBwaW5nJmd0OyBcIjt7WyYqJl19[[/code]

[code]]czoxOlwiIFwiO3tbJiomXX0=[[/code]

[code]]czoxOlwiIFwiO3tbJiomXX0=[[/code]

[code]]czoyMzpcIiAmbHQ7ZmlsdGVyLW1hcHBpbmcmZ3Q7XCI7e1smKiZdfQ==[[/code]

[code]]czo2MzpcIiAmbHQ7ZmlsdGVyLW5hbWUmZ3Q7TXlGYWNlc0V4dGVuc2lvbnNGaWx0ZXImbHQ7L2ZpbHRlci1uYW1lJmd0O1wiO3tbJiomXX0=[[/code]

[code]]czo0NTpcIiAmbHQ7dXJsLXBhdHRlcm4mZ3Q7Ki5qc2YmbHQ7L3VybC1wYXR0ZXJuJmd0O1wiO3tbJiomXX0=[[/code]

[code]]czoyNDpcIiAmbHQ7L2ZpbHRlci1tYXBwaW5nJmd0O1wiO3tbJiomXX0=[[/code]

The myfaces component used to upload file is <t:inputFileUpload>

Some of the attributes used in the demo are:

Value : we should specify the expression referring to UploadedFile type variable of the bean.

Size: The size of the control in characters.

You can find more information about the tag from:

http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_inputFileUpload.html

The value to the tag should be an object of org.apache.myfaces.custom.fileupload.UploadedFile.

Some useful functions of this interface are:

[code]]czoxNjc6XCI8c3Ryb25nPjxhIGhyZWY9XCJodHRwOi8vbXlmYWNlcy5hcGFjaGUub3JnL3RvbWFoYXdrLXByb2plY3QvdG9tYWhhd2sxe1smKiZdfTIvYXBpZG9jcy9vcmcvYXBhY2hlL215ZmFjZXMvY3VzdG9tL2ZpbGV1cGxvYWQvVXBsb2FkZWRGaWxlLmh0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vbWFoYXdrLXByb2plY3QvdG9tYWhhd2sxe1smKiZdfTIvYXBpZG9jcy9vcmcvYXBhY2hlL215ZmFjZXMvY3VzdG9tL2ZpbGV1cGxvYWQvVXBsb2FkZWRGaWxlLmh0bWwjZ2V0U2l6ZSgpXCI+e1smKiZdfWdldFNpemU8L2E+PC9zdHJvbmc+XCI7e1smKiZdfQ==[[/code][code]]czo1NDpcIigpIDogcmV0dXJucyBzaXplIG9mIHRoZSBmaWxlIGJlaW5nIHVwbG9hZGVkIChpbiBsb25nKVwiO3tbJiomXX0=[[/code]

You can find more information about it at:

http://myfaces.apache.org/tomahawk-project/tomahawk12/apidocs/org/apache/myfaces/custom/fileupload/UploadedFile.html

Implementation:

Our bean should define a variable of type UploadedFile. The setter method of this will be called when the page gets submitted and the file contents get populated automatically into this variable. In below example we are saving the file and contents in the server.

Create an EmployeeActionBean  and copy following:

import org.apache.myfaces.custom.fileupload.UploadedFile;

public class EmployeeActionBean {

private UploadedFile imageSrc;

public UploadedFile getImageSrc() {

return imageSrc;

}

public void setImageSrc(UploadedFile imageSrc) {

this.imageSrc = imageSrc;

}

public void uploadFile() {

UploadedFile myFile = getImageSrc();

byte[] buffer = null;

if (myFile != null) {

String extension =  getExtension(myFile);

File outfile = new File(“D:/apache-tomcat-6.0.14/webapps/JSFTest/test.”+extension);

long size = myFile.getSize();

System.out.println(“File Size ==>”+size);

try{

OutputStream out = new FileOutputStream(outfile);

InputStream in = new BufferedInputStream(myFile

.getInputStream());

try {

buffer = new byte[64 * 1024];

int count;

while ((count = in.read(buffer)) > 0)

out.write(buffer, 0, count);

} finally {

in.close();

out.close();

}

}catch(Exception e){

e.printStackTrace();

}

}

}

public static String getExtension(UploadedFile f) {

String ext = null;

String s = f.getName();

int i = s.lastIndexOf(‘.’);

if (i > 0 && i < s.length() – 1) {

ext = s.substring(i + 1).toLowerCase();

}

return ext;

}

}

In JSF page most important thing to be noted is: To add extra enctype=”multipart/form-data” attribute to your h:form tag. This is the MIME type and using this we inform to the server that the input is not a usual form data.

Create a uploadFile.jsp page and copy below contents:

<%@taglib uri=”http://java.sun.com/jsf/html” prefix=”h”%>

<%@taglib uri=”http://java.sun.com/jsf/core” prefix=”f”%>

<%@ taglib uri=”http://myfaces.apache.org/tomahawk” prefix=”t” %>

<f:view>

<h:form id=”uploadFile” enctype=”multipart/form-data”>

click following button to upload the file:

<br/>

<t:inputFileUpload id=”txtFile” value=”#{employeeActionBean.imageSrc}”  size=”70″ > </t:inputFileUpload>

<br />

<h:commandButton action=”#{employeeActionBean.uploadFile}” value=”Submit”>

</h:commandButton>

</h:form>

</f:view>

Now you can upload any file and it will be saved in specified directory in bean by name test.

uploadFile

Be Sociable, Share!
  1. smitha
    October 5th, 2009 at 00:04 | #1

    Please follow this tutorial after the JSF beginners Tutorial if you are new to JSF. To start this tutorial you must have a JSF web application (JSF core) working. This tutorial explains you how you can configure tomahawk in the existing JSF application and use fileupload component.

  2. October 1st, 2009 at 14:21 | #2

    Sonia
    we cannot commit about some other site but you try with the steps mentioned here and you should not be getting any errors.

  3. sonia p
    October 1st, 2009 at 13:52 | #3

    I tried a similar tutorial from this website:
    http://onjava.com/pub/a/onjava/2005/07/13/jsfupload.html?page=4

    and I get the following error:

    Oct 1, 2009 12:53:09 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet jsp threw exception
    java.lang.NoClassDefFoundError: Could not initialize class org.apache.myfaces.shared_tomahawk.config.MyfacesConfig

  1. No trackbacks yet.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes