Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

fileupload interceptor


The fileUpload interceptor automatically works for all the requests that includes files.

We can use this interceptor to control the working of file upload in struts2 such as defining allowed types, maximum file size etc.

Parameters of fileupload interceptor

There are 2 parameters defined for fileupload interceptor.

Parameter Description
maximumSize specifies maximum size of the file to be uploaded.
allowedTypes specifies allowed types. It may be image/png, image/jpg etc.

It automatically adds 3 parameters in the request:

  1. File file represents the file. We can apply methods on this object.
  2. String fileName represents the filename of the file.
  3. String contentType specifies the content type of the file.

Image upload example using struts 2

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

1) Create UserImage.jsp

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

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.Kreationnext;
  2. import java.io.File;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.apache.commons.io.FileUtils;
  5. import org.apache.struts2.interceptor.ServletRequestAware;
  6. import com.opensymphony.xwork2.ActionSupport;
  7. public class FileUploadAction extends ActionSupport implements
  8.         ServletRequestAware {
  9.     private File userImage;
  10.     private String userImageContentType;
  11.     private String userImageFileName;
  12.     private HttpServletRequest servletRequest;
  13.     public String execute() {
  14.         try {
  15. String filePath = servletRequest.getSession().getServletContext().getRealPath(“/”).concat(“userimages”);
  16.         System.out.println(“Image Location:” + filePath);//see the server console for actual location
  17.         File fileToCreate = new File(filePath, this.userImageFileName);
  18.         FileUtils.copyFile(this.userImage, fileToCreate);//copying image in the new file
  19.         return SUCCESS;
  20.     }
  21.     public File getUserImage() {
  22.         return userImage;
  23.     }
  24.     public void setUserImage(File userImage) {
  25.         this.userImage = userImage;
  26.     }
  27.     public String getUserImageContentType() {
  28.         return userImageContentType;
  29.     }
  30.     public void setUserImageContentType(String userImageContentType) {
  31.         this.userImageContentType = userImageContentType;
  32.     }
  33.     public String getUserImageFileName() {
  34.         return userImageFileName;
  35.     }
  36.     public void setUserImageFileName(String userImageFileName) {
  37.         this.userImageFileName = userImageFileName;
  38.     }
  39.     public void setServletRequest(HttpServletRequest servletRequest) {
  40.         this.servletRequest = servletRequest;
  41.     }
  42. }

4) Create struts.xml

This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.xml

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


Output

fileuploadoutput1

fileuploadoutput2

Comments

comments