Struts 2 Upload Multiple File
This demo you will see how to upload multiple file to the server and process it. Finally send back response to the user with name of the file uploaded successfully. Please follow steps below:
- Create maven project name: Struts2UploadMultipleFiles
- Final project structure:
- pom.xml dependency:
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16.3</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.16.3</version> </dependency>
- web.xml:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>struts.devMode</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>struts.custom.i18n.resources</param-name> <param-value>ApplicationResources</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
- index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts 2 Upload Multiple File</title> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script> $(document).ready(function() { $('#addMoreFile').click(function() { $('#uploadTable').append( '<tr><td><s:file label="Choose File to Upload" name="multipleFileUpload" theme="simple"/>'+ '</td></tr>'); return false; }); }); </script> <s:head /> </head> <body> <h3 style="color: green"> <s:text name="Struts 2 Upload Multiple File" /> </h3> <s:form action="uploadMultipleFilesAction" namespace="/" method="post" name="strutsForm" enctype="multipart/form-data"> <s:submit type="button" label="Add more files" id="addMoreFile"/> <table id="uploadTable" border="1"> <tr><td><s:file label="Choose File to Upload" name="multipleFileUpload" theme="simple"/></td></tr> </table> <s:submit align="left" value="Upload All Files"/> </s:form> </body> </html>
- success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts 2 Upload Multiple File</title> </head> <body> <h3 style="color: green"> <s:text name="Below files upload successfully" /> </h3> <table cellspacing="0" cellpadding="0" border="1" width="250"> <tr style="font-weight: bold"> <td><s:text name="Uploaded file name" /></td> </tr> <s:iterator value="uploadedFilesDetailsList" status="stat"> <tr> <td><s:text name="%{#stat.index+1}+'.'"/><s:property value="fileName" /></td> </tr> </s:iterator> </table> </body> </html>
- UploadedFilesDetails.java
package com.javahonk.action; public class UploadedFilesDetails { private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } }
- UploadMultipleFilesAction.java
package com.javahonk.action; import java.io.File; import java.util.ArrayList; import java.util.List; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; @Results({ @Result(name = "error", location = "/index.jsp"), @Result(name = "success", location = "/success.jsp"), @Result(name = "input", location = "/index.jsp") }) public class UploadMultipleFilesAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<File> multipleFileUpload = new ArrayList<File>(); private List<String> multipleFileUploadFileName = new ArrayList<String>(); private List<String> multipleFileUploadContentType = new ArrayList<String>(); private List<UploadedFilesDetails> uploadedFilesDetailsList = new ArrayList<UploadedFilesDetails>(); @Override @Action(value = "/uploadMultipleFilesAction") public String execute() throws Exception { //Print file path for (File file : multipleFileUpload) { System.out.println("File :" + file); } //Iterate file name set in response for (String fileName : multipleFileUploadFileName) { UploadedFilesDetails uploadedFilesDetails = new UploadedFilesDetails(); uploadedFilesDetails.setFileName(fileName); uploadedFilesDetailsList.add(uploadedFilesDetails); } //Print file content type for (String fileContentType : multipleFileUploadContentType) { System.out.println("File type : " + fileContentType); } setUploadedFilesDetailsList(uploadedFilesDetailsList); return ActionSupport.SUCCESS; } public List<File> getMultipleFileUpload() { return multipleFileUpload; } public void setMultipleFileUpload(List<File> multipleFileUpload) { this.multipleFileUpload = multipleFileUpload; } public List<String> getMultipleFileUploadFileName() { return multipleFileUploadFileName; } public void setMultipleFileUploadFileName( List<String> multipleFileUploadFileName) { this.multipleFileUploadFileName = multipleFileUploadFileName; } public List<String> getMultipleFileUploadContentType() { return multipleFileUploadContentType; } public void setMultipleFileUploadContentType( List<String> multipleFileUploadContentType) { this.multipleFileUploadContentType = multipleFileUploadContentType; } public List<UploadedFilesDetails> getUploadedFilesDetailsList() { return uploadedFilesDetailsList; } public void setUploadedFilesDetailsList( List<UploadedFilesDetails> uploadedFilesDetailsList) { this.uploadedFilesDetailsList = uploadedFilesDetailsList; } }
- Run this set up in tomcat server. If you didn’t configure tomcat set up in eclipse yet please use this tutorial: Configure and Run Tomcat server in eclipse. Now right click project –>Run As –> Run on server you will see below page to upload multiple files:
- Using “Add more files” button you could add and upload many files as you want. For demo we have added and selected three files as below:
- Final successful output after all files processed on server:
For more information about file upload in struts 2 application please read this tutorial
Download Project: Struts2UploadMultipleFiles