Home  >  Struts 2

Struts 2 Annotation File Upload Example: Single and Multiple

By Arvind Rai, August 10, 2014
Struts 2 provides efficient way to support file uploads. There are tag library to write view in JSP. Errors can be displayed on UI which is required by the user. Struts 2 provides interceptor to apply required constraints for file upload. We can restrict user to upload certain content types, certain file extensions and maximum size of files. In our example, we are not using strut.xml, we are using annotation to give demo. In this example we will see multiple and single file upload. First we are describing how to do multiple file upload and then we will see how to do single file upload.

Required Software to Run Example

To run the example, we need software as below.
1. JDK 6
2. Eclipse
3. Tomcat 7
4. Maven

Project Structure in Eclipse

Before starting the code walk through, it is beneficiary that first check all the files and their location in project structure. We are not using struts.xml. We are performing our file upload task annotation based.
Struts 2 Annotation File Upload Example: Single and Multiple

File Upload Interceptor : @interceptorRef Annotation

In file upload, we need to apply some constraints. Struts 2 provides below parameters using which we can set constraints on file upload individually.

maximumSize: Sets maximum size (in bytes) which is allowed by interceptor.

allowedTypes: The value for this parameter can be comma separated content types.

allowedExtensions: The value can be comma separated extensions.

Uisng @interceptorRef we can set parameter values as below.

Write Page for Multiple File Upload and Success Message

Struts 2 provides tag library to write views in jsp. Find the JSP how to write code for multiple file upload. We need to take care that name attribute must be same for all File tag.
<%@ taglib prefix="s" uri="/struts-tags" %>
    <h1> Struts 2 File Upload Example </h1>
	<s:form method="post" enctype="multipart/form-data" action="multiUploadFile">
		<s:file label="File One" name="uploads" />
		<s:file label="File Two" name="uploads" />
		<s:file label="FIle Three" name="uploads" />
		<s:submit />
Find the JSP for success messages.
<%@ taglib prefix="s" uri="/struts-tags" %>
You have successfully uploaded the files<br/>
<s:property value="desc"/>

How to Define File Parameter in Struts 2 Action Class for Multiple Upload

Before writing action class we must understand what convention we must follow to get uploaded file values. Struts 2 interceptor automatically defines the properties which we should use in our action class to fetch the uploaded File.

[File Name] : File: java.io.File type object that provides uploaded file data as byte stream.

[File Name]FileName: File name of the File which has been uploaded.

[File Name]ContentType: Content type of File which has been uploaded.

In multiUpload.jsp, file name is "uploads" and we are doing multiple file upload. So we will create List for the property and name in action class will be as below
private List<File> uploads = new ArrayList<File>();
private List<String> uploadsFileName  = new ArrayList<String>();
private List<String> uploadsContentType = new ArrayList<String>(); 

Multiple File Upload Action Class

Now find the action class. We have created a method that will be called when file uploaded page will be submitted. Finally we will save all the uploaded files in a location.
package com.concretepage.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private List<File> uploads = new ArrayList<File>();
	private List<String> uploadsFileName  = new ArrayList<String>();
	private List<String> uploadsContentType = new ArrayList<String>();
	private String desc="";
	@Action( value = "/multiUploadFile",
	public String multiUploadFile() {
		int i=0;
		for(File file : uploads){
			try {
				FileInputStream fis = new FileInputStream(file);
				String fileName = uploadsFileName.get(i);
				String contentType = uploadsContentType.get(i);
    			        FileOutputStream fos = new FileOutputStream("D:/cp/"+fileName);
				byte[] b = new byte[1024];
				while(fis.read(b) != -1){
			    desc += fileName +", ";
			} catch (IOException e) {
	    return SUCCESS;
	public List<File> getUploads() {
		return uploads;
	public void setUploads(List<File> uploads) {
		this.uploads = uploads;
	public List<String> getUploadsFileName() {
		return uploadsFileName;
	public void setUploadsFileName(List<String> uploadsFileName) {
		this.uploadsFileName = uploadsFileName;
	public List<String> getUploadsContentType() {
		return uploadsContentType;
	public void setUploadsContentType(List<String> uploadsContentType) {
		this.uploadsContentType = uploadsContentType;
	public String getDesc() {
		return desc;
	public void setDesc(String desc) {
		this.desc = desc;

Display Error in Struts 2 File Upload : Use <s:fielderror/>

If upload fails, because of file validation like size, content type or extension, the we can catch error. Define input result in action annotation.
and use <s:fielderror/> in error.jsp as below.
<%@ taglib prefix="s" uri="/struts-tags" %>

Struts 2 web.xml

Find the web.xml which we are using in the demo.
  <display-name>Struts 2 File Upload Annotation Example</display-name>

Maven Dependency

Find the maven dependency for struts 2 Jar.

Output of File Upload Example

This is Upload page with multiple file upload option.
Struts 2 Annotation File Upload Example: Single and Multiple
If uploading success, this page will be displayed.
Struts 2 Annotation File Upload Example: Single and Multiple
If there is error, when user has selected the file which is not allowed to upload, the page will be displayed with the file information.
Struts 2 Annotation File Upload Example: Single and Multiple

Struts 2 Single File Upload

For single file upload there are minor changes. Jsp will like
<s:form method="post" enctype="multipart/form-data" action="singleUploadFile">
	<s:file label="File One" name="upload" />
	<s:submit />
And in action class, properties will be declared as below.
private File upload;
private String uploadsFileName;
private String uploadsContentType; 
Create setter and getter for the above properties and follow the multiple file upload action method to save the data.

Download Source Code


©2022 concretepage.com | Privacy Policy | Contact Us