Struts2 File Upload Example

The fileUpload interceptor naturally works for every one of the solicitations that incorporates files.

We can utilize this interceptor to control the working of document transfer in struts2, for example, characterizing permitted types, greatest record estimate etc.

Parameters of fileupload interceptor

There are 2 parameters characterized for fileupload interceptor.

ParameterDescription
maximumSizespecifies greatest size of the document to be uploaded.
allowedTypes determines permitted types. It might be picture/png, picture/jpg etc.

Automatically included parameters

It consequently includes 2 parameters in the request:

  1. String fileName speaks to the filename of the file.
  2. String contentType indicates the substance kind of the file.

The fileName and contentType name depends on the request parameter for the file. If filename is file, you need to use fileFileName and fileContentType. If filename is userImage, you need to use userImageFileName and userImageContentType in the Action class.


Image upload example using struts 2

Let's see the directory structure of file upload application.

file upload in struts 2 directory structure

1) Create UserImage.jsp

This jsp page makes a structure utilizing struts UI labels. It gets document from the client.

index.jsp
  1. <%@ page contentType="text/html; charset=UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.         <title>Upload User Image</title>  
  6.     </head>  
  7.     <body>  
  8.         <h2>  
  9.             Struts2 File Upload & Save Example without Database  
  10.         </h2>  
  11.         <s:actionerror />  
  12.         <s:form action="userImage" method="post" enctype="multipart/form-data">  
  13.             <s:file name="userImage" label="Image" />  
  14.             <s:submit value="Upload" align="center" />  
  15.         </s:form>  
  16.     </body>  
  17. </html>  

2) Create SuccessUserImage.jsp

This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.

SuccessUserImage.jsp
  1. <%@ page contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s"  
  2.     uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.         <title>Success: Upload User Image</title>  
  6.     </head>  
  7.     <body>  
  8.         <h2>  
  9.             Struts2 File Upload Example  
  10.         </h2>  
  11.         User Image: <s:property value="userImage" /><br/>  
  12.         Content Type:<s:property value="userImageContentType" /><br/>  
  13.         File Name:  <s:property value="userImageFileName" /><br/>  
  14.         Uploaded Image: <img src="userimages/<s:property value="userImageFileName"/>"   
  15.                                                      width="100" height="100" />  
  16.     </body>  
  17. </html>  

3) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.

RegisterAction.java
  1. package com.javacodegeeks;  
  2. import java.io.File;  
  3. import javax.servlet.http.HttpServletRequest;  
  4. import org.apache.commons.io.FileUtils;  
  5. import com.opensymphony.xwork2.ActionSupport;  
  6.   
  7. public class FileUploadAction extends ActionSupport{  
  8.     private File userImage;  
  9.     private String userImageContentType;  
  10.     private String userImageFileName;  
  11.   
  12.     public String execute() {  
  13.         try {  
  14. String filePath = ServletActionContext.getServletContext().getRealPath("/").concat("userimages");  
  15.               
  16.         System.out.println("Image Location:" + filePath);//see the server console for actual location  
  17.         File fileToCreate = new File(filePath,userImageFileName);  
  18.         FileUtils.copyFile(userImage, fileToCreate);//copying source file to new file  
  19.               
  20.         return SUCCESS;  
  21.     }  
  22.     public File getUserImage() {  
  23.         return userImage;  
  24.     }  
  25.     public void setUserImage(File userImage) {  
  26.         this.userImage = userImage;  
  27.     }  
  28.     public String getUserImageContentType() {  
  29.         return userImageContentType;  
  30.     }  
  31.   
  32.     public void setUserImageContentType(String userImageContentType) {  
  33.         this.userImageContentType = userImageContentType;  
  34.     }  
  35.     public String getUserImageFileName() {  
  36.         return userImageFileName;  
  37.     }  
  38.     public void setUserImageFileName(String userImageFileName) {  
  39.         this.userImageFileName = userImageFileName;  
  40.     }  
  41. }  

4) Create struts.xml

This xml document characterizes an additional outcome by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.xml
  1.       
  2. <!DOCTYPE struts PUBLIC  
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6.   
  7. <struts>  
  8.     <package name="fileUploadPackage" extends="struts-default">  
  9.         <action name="userImage" class="com.javacodegeeks.FileUploadAction">  
  10.             <interceptor-ref name="fileUpload">  
  11.                 <param name="maximumSize">2097152</param>  
  12.   
  13.                 <param name="allowedTypes">  
  14.                     image/png,image/gif,image/jpeg,image/pjpeg  
  15.                                 </param>  
  16.             </interceptor-ref>  
  17.             <interceptor-ref name="defaultStack"></interceptor-ref>  
  18.             <result name="success">SuccessUserImage.jsp</result>  
  19.             <result name="input">UserImage.jsp</result>  
  20.         </action>  
  21.     </package>  
  22. </struts>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition