Struts 2 Upload Single File
This demo you will see how to upload single file to the server in struts 2 application and upon processing file successfully on server side client will receive response back if file process or not. Please follow steps below:
Note: If you are looking to upload multiple file to the server using struts 2 please read this tutorial
- Create maven project name: Struts2SingleFileUpload
- Final project structure:
- pom.xml:
<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>
- 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 Single File</title> <s:head /> </head> <body> <h3 style="color: green"> <s:text name="Struts 2 Upload Single File" /> </h3> <s:form action="uploadSingleFilesAction" namespace="/" method="post" name="strutsForm" enctype="multipart/form-data"> <s:file label="Choose File to Upload" name="multipleFileUpload" theme="simple"/> <s:submit align="left" value="Upload File"/> </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="File name" /></td> <td><s:text name="File Path" /></td> <td><s:text name="File Content Type" /></td> </tr> <s:iterator value="singleFileUploadDetailsList" status="stat"> <tr> <td><s:property value="fileName" /></td> <td><s:property value="filePath" /></td> <td><s:property value="fileContentType" /></td> </tr> </s:iterator> </table> </body> </html>
- SingleFileUploadDetails.java
package com.javahonk.action; public class SingleFileUploadDetails { private String filePath; private String fileContentType; private String fileName; public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } }
- UploadSingleFilesAction.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 UploadSingleFilesAction 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<SingleFileUploadDetails> singleFileUploadDetailsList = new ArrayList<SingleFileUploadDetails>(); @Override @Action(value = "/uploadSingleFilesAction") public String execute() throws Exception { SingleFileUploadDetails singleFileUploadDetails = new SingleFileUploadDetails(); //Print file path for (File file : multipleFileUpload) { singleFileUploadDetails.setFilePath(file.getPath()); } for (String fileContentType : multipleFileUploadContentType) { singleFileUploadDetails.setFileContentType(fileContentType); } for (String fileName : multipleFileUploadFileName) { singleFileUploadDetails.setFileName(fileName); } singleFileUploadDetailsList.add(singleFileUploadDetails); setSingleFileUploadDetailsList(singleFileUploadDetailsList); 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<SingleFileUploadDetails> getSingleFileUploadDetailsList() { return singleFileUploadDetailsList; } public void setSingleFileUploadDetailsList( List<SingleFileUploadDetails> singleFileUploadDetailsList) { this.singleFileUploadDetailsList = singleFileUploadDetailsList; } }
- 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 single file:
- Choose file to upload the click Upload File button:
- Finally you will see below page
For more information please read tutorial for struts 2 site here:
Download Project: Struts2SingleFileUpload