Struts 2 framework upload multiple files example

Struts 2 framework upload multiple files example

Previous Home Next

 

The struts2 framework provide facility to upload multiple file upload on the server . The multiple file add on list and display on the browser.
The file component provide to choose multiple file which are following:

<s:file label="File 1" name="fileUpload" size="40" />
<s:file label="File 2" name="fileUpload" size="40" />
<s:file label="FIle 2" name="fileUpload" size="40" />

 
Directory Structure of Multiple <s:file> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<h2>Multiple File Upload Example in Struts2 Framework</h2>
<s:actionerror/>
<s:form action="file" method="post" enctype="multipart/form-data">
<s:file name="file" label="File Attached-A" size="40"/>
<s:file name="file" label="File Attached-B" size="40"/>
<s:file name="file" label="File Attached-C" size="40"/>
<s:file name="file" label="File Attached-D" size="40"/>
<s:submit></s:submit>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="" class="org.r4r.MultipleFileUploadAction" method="display">
<result name="none" type="dispatcher">/index.jsp</result>
</action>
<action name="file" class="org.r4r.MultipleFileUploadAction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>

MultipleFileUploadAction.java

package org.r4r;

import java.io.File;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class MultipleFileUploadAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	List<File> file;
	List<String> fileContentType;
	List<String> fileFileName;
	
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String execute(){
		return "success";
	}
	public String display(){
		return "none";
	}

}

success.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<h2>Multiple File Upload Example in Struts2 Framework</h2>
<b>
File:-<s:property value="file"/>
<br/>Content Type:-<s:property value="fileContentType"/>
<br/>File Name:-<s:property value="fileFileName"/>
</b>

Output






Previous Home Next