<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Upload User Image</title> </head> <body> <h2>Struts2 File Upload & Save Example</h2> <s:actionerror /> <s:form action="userImage" method="post" enctype="multipart/form-data"> <s:file name="userImage" label="User Image" size="10"/> <s:submit value="Upload" align="center" /> </s:form> </body> </html>
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Success: Upload User Image</title> </head> <body> <h2>Struts2 File Upload Example</h2> User Image: <s:property value="userImage"/> <br/> Content Type: <s:property value="userImageContentType"/> <br/> File Name: <s:property value="userImageFileName"/> <br/> Uploaded Image: <br/> <img src="<s:property value="userImageFileName"/>"/> </body> </html>
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>File Upload Error</title> </head> <body> There has been an error in uploading the file. </body> </html>
package com.dineshonjava.struts2.action.upload; import java.io.File; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; /** * @author Dinesh Rajput * */ public class FileUploadAction extends ActionSupport implements ServletRequestAware { private static final long serialVersionUID = 1L; private File userImage; private String userImageContentType; private String userImageFileName; private HttpServletRequest servletRequest; public String execute() { try { String filePath = servletRequest.getSession().getServletContext().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; } }In above class file we have declared few attributes:
<?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> <constant name="struts.devMode" value="true" /> <package name="upload" extends="struts-default"> <action name="userImage" class="com.dineshonjava.struts2.action.upload.FileUploadAction"> <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">success.jsp</result> <result name="input">index.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts>Note that in above entry we have specified two parameter to fileUpload interceptor, maximumSize and allowedTypes. These are optional parameters that we can specify to interceptor. The maximumSize param will set 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. Here we have specified it to be an image file (image/png,image/gif,image/jpeg,image/pjpeg).
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <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>index.jsp</welcome-file> </welcome-file-list> </web-app>
Labels: struts2