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“.

package com.mkyong.common.action;


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”.


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

<h1>Struts 2 <s:file> 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" />



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

<h1>Struts 2 <s:file> file upload example</h1>

<div class="ads-in-post hide_if_width_less_800">
<script async src="//"></script>
<!-- new 728x90 - After2ndH4 -->
<ins class="adsbygoogle hide_if_width_less_800" 
(adsbygoogle = window.adsbygoogle || []).push({});
   File Name : <s:property value="fileUploadFileName"/> 

   Content Type : <s:property value="fileUploadContentType"/> 

   File : <s:property value="fileUpload"/> 


3. struts.xml

Link it all ~

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

 	<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 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 name="params">
                <param name="excludeParams">dojo\..*,^struts\..*</param>
            <interceptor-ref name="validation">
                <param name="excludeMethods">input,back,cancel,browse</param>
            <interceptor-ref name="workflow">
                <param name="excludeMethods">input,back,cancel,browse</param>
	    <result name="success">pages/result.jsp</result>
	    <result name="input">pages/fileupload.jsp</result>

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


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.


  1. Struts 2 file documentation