To upload a file,the interface org.apache.struts.upload.FormFile is used in the application.
To represents a file that has been uploaded by the client,this interface is used.
A D V E R T I S E M E N T
It is the only class or interface in the upload package which is typically referenced directly by a Struts application
Creating Form Bean
In our example,the form bean class contains one property theFile, which is of type org.apache.struts.upload.FormFile.
The following example is the code of the FormBean (StrutsUploadForm.java):
/**
* Form bean for Struts File Upload.
*
*/
public class StrutsUploadForm extends ActionForm
{
private FormFile theFile;
/**
* @return Returns the theFile.
*/
public FormFile getTheFile()
{
return theFile;
}
/**
* @param theFile The FormFile to set.
*/
public void setTheFile(FormFile theFile)
{
this.theFile = theFile;
}
}
Creating Action Class
To retrieve the reference of the uploaded file,the action class simply calls getTheFile() function on the FormBean object.
To get uploaded file and its information, the reference of the FormFile is used .
The following is code of our action class called strutsUploadAction.java:
In index.jsp to call the form,add the following line.
<li>
<html:link page="/pages/FileUpload.jsp">Struts
File Upload</html:link>
<br>
Example shows you how to Upload File with
Struts.
</li>
Building Example and Testing
Go to Struts\strutstutorial directory and type ant on the command prompt,to build and deploy application.
This will help to deploy application. Open the browser and type FileUpload.jsp page.
Your browser should display the file upload form as follows: