HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

java-struts: How to upload files in struts 2?

i want to upload file in a folder resided in root folder. and file name in database using struts2. Please help me.

java x 210
struts x 31
Posted On : 2014-04-18 23:26:23.0
profile Garima Gupta - Garima Gupta


To upload file in struts2 we need to use struts2 built-in FileUploadInterceptor. The File Upload Interceptor is based on MultiPartRequestWrapper, which is applied automatically to the current request if it contains the file element.
Before we implement the concept, we need to be sure that commons-io.jar file is present in the classpath. Following are the required Jar files to upload files using struts2.

required jar files to upload file in struts2

Now Let´s see the action class source code. Which adds the file upload functionality to our application. Create a java class as follows named

package in.anyforum;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements
ServletRequestAware {
private File userImage;
private String userImageContentType;
private String userImageFileName;

private HttpServletRequest servletRequest;

public String execute() {
try {
String filePath = servletRequest.getSession().getServletContext().getRealPath(?/?).concat("Folder_Name resided in ROOT");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.userImageFileName);
/*** this.userImageFileName returns the file name, You can put database connection code to insert the file name into database below *****/
FileUtils.copyFile(this.userImage, fileToCreate);
} catch (Exception e) {

return INPUT;
return SUCCESS;

public File getUserImage() {
return userImage;

public void setUserImage(File userImage) {
this.userImage = userImage;

public String getUserImageContentType() {
return userImageContentType;

public void setUserImageContentType(String userImageContentType) {
this.userImageContentType = userImageContentType;

public String getUserImageFileName() {
return userImageFileName;

public void setUserImageFileName(String userImageFileName) {
this.userImageFileName = userImageFileName;

public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;



In above class file we have declared few attributes:

private File userImage: It contains actual uploaded File.
private String userImageContentType: This string contains the Content Type of uploaded file.
private String userImageFileName: This string contains the file name of uploaded file.

Note: we have implemented interface org.apache.struts2.interceptor.ServletRequestAware in above action class. This is to get servletRequest object to get the real path. We have used FileUtil.copyFile() method of commons-io package to copy the uploaded file on a location as you mentioned in String filePath variable.

Now let´s see the code the for input form and other JSP pages:


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Upload File</title>

<h2>Struts2 File Upload Example-</h2>
<s:actionerror />
<s:form action="userImage" method="post" enctype="multipart/form-data">
<s:file name="userImage" label="User Image" />
<s:submit value="Upload" align="center" />


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Success: Uploading File</title>
<h2>Struts2 File Upload Example</h2>

File Name: <s:property value="userImageFileName"/> Uploaded successfully.
Uploaded Image:
<img src="/Folder_Name resided in ROOT/<s:property value="userImageFileName"/>"/>



<action name="userImage"
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">Success.jsp</result>
<result name="input">FileUpload.jsp</result>

Note: In above entry as we have specified two parameter to fileUpload interceptor, maximumSize and allowedTypes. These are optional parameters that we can specify to interceptor. The maximumSize param sets the maximum file size that can be uploaded. By default this is 2MB. And the allowedTypes param specify the allowed content types of file which can be uploaded.

As in above example we are uploading image so we mention some acceptable image types. You can modify it as per your need. and also it´s maximum size.

Posted On : 2014-04-19 00:25:52
Satisfied : 2 Yes  0 No
profile Saksham Kumar - Saksham Kumar
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -