In Struts 2, the <s:file> tag is used to create a HTML file upload component to allow users select file from their local disk and upload it to the server. In this tutorial, you will create a JSP page with file upload component, set the maximum size and allow content type of the upload file, and display the uploaded file details.

1. Action class

Action class for the file upload, declare a “File” variable to store the user uploaded file, two String variables to store the file name and content type. The “fileUpload interceptor” will auto inject the uploaded file detail via set”X”ContentType() and set”X”FileName(), make sure the method name is spell correctly.

P.S X is the variable to store the uploaded file.

The file upload function is depends on the “fileUpload Interceptor“, make sure it is included in the Action’s stack. The lucky is, the default stack is already includes the “fileUpload Interceptor“.

FileUploadAction.java

package com.mkyong.common.action;
 
import java.io.File;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class FileUploadAction extends ActionSupport{
 
	private File fileUpload;
	private String fileUploadContentType;
	private String fileUploadFileName;
 
	public String getFileUploadContentType() {
		return fileUploadContentType;
	}
 
	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}
 
	public String getFileUploadFileName() {
		return fileUploadFileName;
	}
 
	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}
 
	public File getFileUpload() {
		return fileUpload;
	}
 
	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}
 
	public String execute() throws Exception{
 
		return SUCCESS;
 
	}
 
	public String display() {
		return NONE;
	}
 
}

2. Result Page

Use <s:file> tag to render a file upload component, and set the form enctype type to “multipart/form-data”.

fileupload.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
 
<body>
<h1>Struts 2 &lt;s:file&gt; file upload example</h1>
 
<s:form action="resultAction" namespace="/" 
method="POST" enctype="multipart/form-data">
 
<s:file name="fileUpload" label="Select a File to upload" size="40" />
 
<s:submit value="submit" name="submit" />
 
</s:form>
 
</body>
</html>

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 
<body>
<h1>Struts 2 &lt;s:file&gt; file upload example</h1>
 
<h4>
   File Name : <s:property value="fileUploadFileName"/> 
</h4> 
 
<h4>
   Content Type : <s:property value="fileUploadContentType"/> 
</h4> 
 
<h4>
   File : <s:property value="fileUpload"/> 
</h4> 
 
</body>
</html>

3. struts.xml

Link it all ~

<?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" />
 	<constant name="struts.custom.i18n.resources" value="global" />
 
	<package name="default" namespace="/" extends="struts-default">
 
	<action name="fileUploadAction" 
	    class="com.mkyong.common.action.FileUploadAction" method="display">
	    <result name="none">pages/fileupload.jsp</result>
	</action>
 
	<action name="resultAction" class="com.mkyong.common.action.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">pages/result.jsp</result>
	    <result name="input">pages/fileupload.jsp</result>
 
	</action>
   </package>	
</struts>

File Size Limits
In this example, you set the upload file size limit via “fileUpload interceptor“, this value is count in bytes. In this case, the maximum size of the upload file is 10kb.

The default maximum file size of the upload file is 2MB

File Types
You can set the allow file type via “fileUpload interceptor” as well. In this case, the upload file only accept the “text/plain” content type.

In Struts 2, there are may ways to lead same Rome, make sure you check this Struts 2 file upload documentation.

4. Demo

http://localhost:8080/Struts2Example/fileUploadAction.action

Struts 2 file upload example

Error message is prompt if you upload a file which is more than 10kb, or not a text file.

Struts 2 file upload error page

Upload a text file named “XWORK-LICENSE.txt”, file size : 5kb.

Struts 2 file upload example

The uploaded file will be treat as a temporary file, with a long random file name, upload__376584a7_12981122379__8000_00000010.tmp. Make sure you copy this temp file to somewhere else. Read FileUtils documentation to copy files easily.

Reference

  1. Struts 2 file documentation
  2. http://struts.apache.org/2.0.14/docs/file-upload.html
  3. http://struts.apache.org/2.0.14/docs/how-do-we-upload-files.html
  4. http://commons.apache.org/io/api-1.4/org/apache/commons/io/FileUtils.html
  5. http://www.mkyong.com/struts/struts-file-upload-example/
Tags :
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts