In this section, you will learn how to upload Image using Struts2 Interceptor. This section contain Struts 2 Image Upload Interceptor example with downloadable code.
Struts 2 Image Upload using Interceptor
In this section, you will learn how to upload Image using Struts2 Interceptor. This section contain Struts 2 Image Upload Interceptor example with downloadable code.
The project hierarchy is given below :

The jar file used in the application is given below :

CODE
In this example, a form for uploading image is available which gives the information of the uploaded image after upload.
The code for the JSP files are given below :
ImageUpload.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Upload User Image</title> </head> <body> <h2>Struts2 Image Upload</h2> <s:actionerror /> <s:form action="userImage.action" method="post" enctype="multipart/form-data"> <s:file name="userImage" label="User Image" /> <s:submit value="Upload" align="center" /> </s:form> </body> </html>
ImageUploadSuccess.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Struts2 Image Upload</title> </head> <body> <h2>Struts2 Image Upload</h2> <b>Image Uploaded To Folder :</b><s:property value="userImage"/> <br/> <b>Content Type:</b> <s:property value="userImageContentType"/> <br/> <b>Uploaded Image Name:</b> <s:property value="userImageFileName"/> <br/> <b>Uploaded Image Preview :</b> <br/> <img src="<s:property value="userImageFileName"/>"/> </body> </html>
The code for the action class is given below :
ImageUploadAction.java
package com.devmanuals.struts2;
import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class ImageUploadAction extends ActionSupport implements
ServletRequestAware {
private File userImage;
private String userImageContentType;
private String userImageFileName;
private HttpServletRequest servletRequest;
public String execute() {
try {
HttpSession session = servletRequest.getSession();
ServletContext context = session.getServletContext();
String filePath = context.getRealPath("/");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.userImageFileName);
FileUtils.copyFile(this.userImage, fileToCreate);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
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;
}
@Override
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}
}
The xml file configurations are given below :
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2FileUpload</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>ImageUpload.jsp</welcome-file> </welcome-file-list> </web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <action name="userImage" class="com.devmanuals.struts2.ImageUploadAction"> <interceptor-ref name="fileUpload"> <param name="maximumSize">2097152</param> <param name="allowedTypes"> image/png,image/gif,image/jpeg,image/pjpeg </param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="success">ImageUploadSuccess.jsp</result> <result name="input">ImageUpload.jsp</result> </action> </package> </struts>
OUTPUT
The first form will be the upload form as given below :

After uploading image, it will display following information on success page :


[ 0 ] Comments