Struts 2 file upload example

Struts 2 file upload example

Previous Home Next

 

The struts2 framework provide file tag to upload any file on the server. Through this tag to select the file then submit the file on the server and set the contentType , fileName or set size. 
The action class upload the file and return the string and contentType or fileName. If file size is large then return input on the browser.
The Struts 2 file component provide the following <s:file></s:file> tag:
<s:file name="file" label="Select a File to upload" size="40" />

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



index.jsp

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

<h2>File Upload Tag Example in Struts2 Framework</h2>
<s:actionerror/>
<s:form action="file" method="post" enctype="multipart/form-data">
<s:file name="file" label="File Attached" 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.FileUploadAction" method="display">
<result name="none" type="dispatcher">/index.jsp</result>
</action>
<action name="file" class="org.r4r.FileUploadAction">
<interceptor-ref name="exception" />
<interceptor-ref name="i18n" />
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/plain</param>
<param name="maximumSize">10240</param>
</interceptor-ref>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>

FileUploadAction.java

package org.r4r;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

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

}

success.jsp

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

<h2>File Upload Tag 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