Friday, May 28, 2010

Uploading Files: ADF InputFile Component

In this post we will look at how the InputFile component can be used to upload files to the app server. Once the handle of a file is available at the app server then that file can processed in any way desirable. It can be stored on a file system or on a database or sent over a web service message.

Create a JSF page and drag a Input File component available in ADF Faces common components pallete, onto the page.

Next create a backing bean (faces context). Import the class: org.apache.myfaces.trinidad.model.UploadedFile; This class object will hold the reference to the file that will be uploaded using this component.

Here is the sample code for this backing bean:

import org.apache.myfaces.trinidad.model.UploadedFile;

public class FileUploader {
UploadedFile myFile;
public FileUploader() {
}
public String uploadFile() {
System.out.println("inside fileUpload method");
return null;
}
public void setMyFile(UploadedFile myFile) {
this.myFile = myFile;
System.out.println("inside set uploaded file method");
System.out.println("filename is : "+file.getFilename());
System.out.println("type is : "+file.getContentType());
System.out.println("filesize is : "+file.getLength());
}
public UploadedFile getMyFile() {
return myFile;
}
}

Set the value property, of the Input File component to point to the public instance variable myFile of the backing bean class created above. This variable will hold the reference to the file being uploaded.

Next add a push botton to the page and point its action to the uploadFile() method above. In this method you can write the code to process the file that has been uploaded.


No comments:

Post a Comment