Struts 2 File Upload

In this section you will learn how to write program in Struts 2 to upload the file on the server. In this example we are also providing the code to save the uploaded file in any directory on the server machine.

Struts 2 File Upload

Struts 2 File Upload


In this section you will learn how to write program in Struts 2 to upload the file on the server. In this example we are also providing the code to save the uploaded file in any directory on the server machine.

The Struts 2 FileUpload component can be used to upload the multipart file in your Struts 2 application. In this section you will learn about the code to upload file on the server.

How File Upload Works in Struts 2?

Struts 2 utilizes the service of  File Upload Interceptor to add the support for uploading files in the Struts applications. The Struts 2 File Upload Interceptor is based on MultiPartRequestWrapper, which is automatically applied to the request if it contains the file element. Then it adds the following parameters to the request (assuming the uploaded file name is MyFile). 

  • [MyFile] : File - the actual File
  • [MyFile]ContentType : String - the content type of the file
  • [File Name]FileName : String - the actual name of the file uploaded (not the HTML name)

In the action class you can get the file, the uploaded file name and content type by just creating getter and setters. For example, setMyfile(File file), setMyFileContentType(String contentType), getMyFile(), etc..

The file upload interceptor also does the validation and adds errors, these error messages are stored in the file. The values of the messages can be overridden by providing the text for the following keys:

  • struts.messages.error.uploading - error when uploading of file fails
  • struts.messages.error.file.too.large - error occurs when file size is large
  • struts.messages.error.content.type.not.allowed - when the content type is not allowed


You can use the following parameters to control the file upload functionality.

  • maximumSize This parameter is optional. The default value of this is 2MB.
  • allowedTypes This parameter is also optional. It allows you to specify the allowed content type.

Writing Example code to upload file

Now we will write the code to upload the file on server.

Action Class

In the action class we will define four properties to facilitate the file upload.

private File upload; // The actual file

private String uploadContentType;// The content type of the file

private String uploadFileName;// The uploaded file name and path

private String fileCaption;// The caption of the file entered by user.

Here is the full code of action class

package net.roseindia;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsFileUpload extends ActionSupport {
  private File upload;//The actual file
  private String uploadContentType; //The content type of the file
  private String uploadFileName; //The uploaded file name
  private String fileCaption;//The caption of the file entered by user
  public String execute() throws Exception {

  return SUCCESS;

  public String getFileCaption() {
  return fileCaption;
  public void setFileCaption(String fileCaption) {
  this.fileCaption = fileCaption;
  public File getUpload() {
  return upload;
  public void setUpload(File upload) {
  this.upload = upload;
  public String getUploadContentType() {
  return uploadContentType;
  public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
  public String getUploadFileName() {
  return uploadFileName;
  public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;


Here we have not shown the code to save the uploaded file. But it can be done easily using the following code in execute(..) method of action class. Here is code snippet.

//Following code can be used to save the uploaded file

try {

String fullFileName = "c:/upload/myfile.txt";

File theFile = new File(fullFileName);

FileUtils.copyFile(upload, theFile);

} catch (Exception e) {


return INPUT;


Writing JSP page

Here is the code of jsp file (upload.jsp) that displays the file upload form to the user.

<%@ taglib prefix="s" uri="/struts-tags" %>
<title>File Upload Example</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/>



<s:actionerror />
<s:fielderror />
<s:form action="doUpload" method="POST" enctype="multipart/form-data">
<td colspan="2"><h1>File Upload Example</h1></td>

<s:file name="upload" label="File"/>
<s:textfield name="caption" label="Caption"/>
<s:submit />

In the above code the form encrypt type is "multipart/form-data" and <s:file ../> tag renders the html file tag.

File upload success page

Here is the code of file upload(upload-success.jsp) success page.

<%@ page 
contentType="text/html; charset=UTF-8"
<%@ taglib prefix="s" uri="/struts-tags" %>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/>

<table class="wwFormTable">

<td colspan="2"><h1>File Upload Example</h1></td>

<td class="tdLabel"><label for="doUpload_upload" class="label">Content Type:</label></td>
<td><s:property value="uploadContentType" /></td>

<td class="tdLabel"><label for="doUpload_upload" class="label">File Name:</label></td>
<td ><s:property value="uploadFileName" /></td>

<td class="tdLabel"><label for="doUpload_upload" class="label">File:</label></td>
<td><s:property value="upload" /></td>

<td class="tdLabel"><label for="doUpload_upload" class="label">File Caption:</label></td>
<td><s:property value="caption" /></td>



Adding mapping in struts.xml file

Add the following mapping in the struts.xml file.

<!-- File Upload -->

<action name="showUpload">

<action name="doUpload" class="net.roseindia.StrutsFileUpload">
<result name="input">/pages/upload.jsp</result>

<!-- End File Upload -->


The "showUpload" action displays the upload form and "doUpload" action actually uploads the file.

Running the example

To test the application compile code and then run the tomcat. Type http://localhost:8080/struts2tutorial/roseindia/showUpload.action in your browser. You browser should show the following form:



Now browse the file, enter caption and then click on the "Submit" button. Application will upload your file and then following success screen will be displayed.



There is one important point to be noted about File Upload Interceptor. The File Upload Interceptor actually deletes the the upload, once the action is executed. Here is the screen shot of tomcat that shows the file delete message:

INFO: Removing file upload C:\apache-tomcat-6.0.10Struts2\apache-tomcat-6.0.10\work\Catalina\

In this section you learnt the concept of file upload in struts 2.





  1. Validations using Struts 2 Annotations
  2. Struts 2 Features
  3. Struts 2 - History of Struts 2
  4. Struts 2 Architecture - Detail information on Struts 2 Architecture
  5. Download and Installing Struts 2
  6. Struts 2 Hello World Application Example, Learn how to develop Hello World application in struts 2.
  7. Developing JSP, Java and Configuration for Hello World Application
  8. Struts Configuration file - struts.xml
  9. Introduction to Struts 2 Tags
  10. Struts2 Actions
  11. Static Parameter
  12. Accessing Session Object
  13. Access Request and Response
  14. Control Tags-If / Else If / Else
  15. Append Tag (Control Tags) Example
  16. Generator Tag (Control Tags) Example
  17. Generator Tag (Control Tags) Using Count Attributes
  18. Generator Tag (Control Tags) Using an Iterator with Id Attributes
  19. Iterator Tag (Control Tags) Example
  20. Merge Tag (Control Tags) Example
  21. Subset Tag (Control Tags) Example
  22. Subset Tag (Control Tags) Example Using Count
  23. Subset Tag (Control Tags) Example Using Start
  24. Action Tag (Data Tag) Example
  25. Bean Tag (Data Tag) Example
  26. Date Tag (Data Tag) Example
  27. Include Tag (Data Tag) Example
  28. Param Tag (Data Tag) Example
  29. Set Tag (Data Tag) Example
  30. Text Tag (Data Tag) Example
  31. Property Tag (Data Tag) Example
  32. Checkbox Tag (Form Tag) Example
  33. Checkboxlist Tag (Form Tag) Example
  34. Datetimepicker Tag (Form Tag) Example
  35. Doubleselect Tag (Form Tag) Example
  36. File Tag (Form Tag) Example
  37. Form Tag Example
  38. Label Tag (Form Tag) Example
  39. Optiontransferselect Tag (Form Tag) Example
  40. Optgroup Tag (Form Tag) Example