Struts 2 File Upload Example

In this Struts 2 tutorial, we will help you to upload a file in Struts 2 application. In Struts 2, <s:file> tag is used to create a file upload component. Every file upload request passes through the fileUpload interceptor. This interceptor help to control the file upload request like controlling file size, file type, etc.

In this example, we have used 2 parameters of fileUpload interceptor.

  1. allowedTypes: control the type of file like jpg, zip, txt, etc
  2. maximumSize: control the size of the file.

Software Used

  1. Eclipse IDE
  2. Tomcat 8
  3. JDK 8

Dependencies Required

To upload the file in your Struts 2 application, you must have below dependencies in your build path.

  1. Commons-FileUpload
  2. Commons-IO

If you are using Maven then you can add these libraries as dependencies in your project’s pom.xml.


Note: fileUpload interceptor will automatically inject the uploaded file detail while setting the name like imageFileName and imageContentType.

Project Structure in Eclipse

Struts 2 File Upload Example


Adding the Struts 2 filter in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns=""
	xsi:schemaLocation="" id="WebApp_ID" version="3.1">

Action Class

Inside the action, we need to declare the input variable and define the business logic or how to handle the incoming file.
package org.websparrow;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

public class SingleFileUploadAction extends ActionSupport {
	private static final long serialVersionUID = -7116807617477612442L;
	private File img;
	private String imgContentType;
	private String imgFileName;

	public String execute() throws Exception {

		try {
			// get the context path to save the file
			String path = ServletActionContext.getServletContext().getRealPath("/");
			System.out.println("Image Location:" + path);

			// handling the file
			File file = new File(path, imgFileName);
			FileUtils.copyFile(img, file);

		} catch (Exception e) {
		return "SUCCESS";

	public File getImg() {
		return img;

	public void setImg(File img) {
		this.img = img;

	public String getImgContentType() {
		return imgContentType;

	public void setImgContentType(String imgContentType) {
		this.imgContentType = imgContentType;

	public String getImgFileName() {
		return imgFileName;

	public void setImgFileName(String imgFileName) {
		this.imgFileName = imgFileName;

JSP Page

Define your user interface page and define the enctype="multipart/form-data" in form attribute.

Note: enctype=”multipart/form-data” must defined with method="POST".

<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<title>Struts 2 File Upload Example</title>
	<h1>Struts 2 File Upload Example</h1>

	<s:form action="SingleFileUpload" method="post" enctype="multipart/form-data">
		<s:file name="img" label="Select Image"></s:file>
		<s:submit value="Upload"></s:submit>

Displaying the SUCCESS and ERROR response.

<%@ taglib prefix="s" uri="/struts-tags"%>
	<h2>Struts 2 File Upload Example</h2>
	<s:property value="img" />
	<br />
	Content Type:
	<s:property value="imgContentType" />
	<br />
	Uploaded Image:
	<br />
	<img src="<s:property value='imgFileName'/>" width="500" height="400" />
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
	<s:fielderror />


Inside the struts.xml we need to map the SUCCESS and ERROR responses and fileUpload interceptor.

	<package name="default" extends="struts-default">
		<action name="SingleFileUpload" class="org.websparrow.SingleFileUploadAction">
			<interceptor-ref name="fileUpload">
				<param name="maximumSize">2097152</param>

				<param name="allowedTypes">
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<result name="SUCCESS">wel-for-single.jsp</result>
			<result name="input">error.jsp</result>


Now you are done and run you application and make sure you are allowed to upload png, gif, jpeg, pjpeg type of file and maximum size of the file is 2MB.

If images are not in dedicated directories, go to image location printed in console.

Screen 1

Struts 2 File Upload Example

Screen 2

Struts 2 File Upload Example

Screen 3

Struts 2 File Upload Example

Download Source Code –

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.