Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

The Struts 2 framework provides built-in support for processing file uploads that conform to RFC 1867, "Form-based File Upload in HTML". When correctly configured the framework will pass uploaded file(s) into your Action class. Support for individual and multiple file uploads are provided. When a file is uploaded it will typically be stored in a temporary directory. Uploaded files should be processed or moved by your Action class to ensure the data is not lost. Be aware that servers may have a security policy in place that prohibits you from writing to directories other than the temporary directory and the directories that belong to your web application.

Table of ContentsminLevel2

Dependencies

The Struts 2 framework leverages add-on libraries to handle the parsing of uploaded files. These libraries are not included in the Struts distribution, you must add them into your project. The libraries needed are:

Library

URL

Struts 2.0.x

Struts 2.1.x

Struts 2.5.x

Commons-FileUpload

http://commons.apache.org/fileupload/

1.1.1

1.2.1

1.3.2

Commons-IO

http://commons.apache.org/io/

1.0

1.3.2

2.4

If you are using Maven then you can add these libraries as dependencies in your project's pom.xml.

...

...

Basic Usage

The org.apache.struts2.interceptor.FileUploadInterceptor class is included as part of the defaultStack. As long as the required libraries are added to your project you will be able to take advantage of of the Struts 2 fileUpload capability. Configure an Action mapping for your Action class as you typically would.

Example action mapping:

...

...

A form must be create with a form field of type file, <INPUT type="file" name="upload">. The form used to upload the file must have its encoding type set to multipart/form-data, <FORM action="doUpload" enctype="multipart/form-data" method="post">. The standard procedure for adding these elements is by using the Struts 2 tag libraries as shown in the following example:

Example JSP form tags: Wiki Markup{snippet{snippet:id=example-form|lang=xml|javadoc=true|url=org.apache.struts2.interceptor.FileUploadInterceptor}The fileUpload interceptor will use setter injection to insert the uploaded file and related data into your Action class. For a form field named upload you would provide the three setter methods shown in the following example:

Example Action class:

...

...

The purpose of each one of these methods is described in the table below. Notice that if you have multiple file form elements with different names you would be required to have another corresponding set of these methods for each file uploaded.

...

multipleUploadUsingArray.jsp Notice all file input types have the same name.

...

...

MultipleFileUploadUsingArrayAction.java

 

...


...

 

Uploading Multiple Files using Lists

multipleUploadUsingList.jsp Notice all file input types have the same name.

...

...

MultipleFileUploadUsingListAction.java

...

java

...

Advanced Configuration

The Struts 2 default.properties file defines several settings that affect the behavior of file uploading. You may find in necessary to change these values. The names and default values are:

...

...

Please remember that the struts.multipart.maxSize is the size limit of the whole request, which means when you uploading multiple files, the sum of their size must be below the struts.multipart.maxSize!

In order to change theses settings you define a constant in your applications struts.xml file like so:

...

...

Additionally the fileUpload interceptor has settings that can be put in place for individual action mappings by customizing your interceptor stack.

...

...

File Size Limits

There are two separate file size limits. First is struts.multipart.maxSize which comes from the Struts 2 default.properties file. This setting exists for security reasons to prohibit a malicious user from uploading extremely large files to file up your servers disk space. This setting defaults to approximately 2 megabytes and should be adjusted to the maximum size file (2 gigs max) that your will need the framework to receive. If you are uploading more than one file on a form the struts.multipart.maxSize applies to the combined total, not the individual file sizes. The other setting, maximumSize, is an interceptor setting that is used to ensure a particular Action does not receive a file that is too large. Notice the locations of both settings in the following example:

...

...

File Types

There are two ways to limit the uploaded file type, declaratively and programmatically. To declaratively limit the file type a comma separated list of allowedTypes can be specified as a fileUpload interceptor param as shown in the following example:

...

...

When the uploaded file type does not match one of the MIME types specified a field error will be created as described in the next section entitled Error Messages. Programmatically limiting the file type means using the information passed in to your Action class via the setXContentType(String contentType) method. The benefit to this type of approach would be that it's more flexible and no interceptor configuration would be needed if file sizes are keep under 2 megs.

...

The struts.multipart.validationRegex is used to define a RegEx to be used to validate if the incoming request is a multipart request. The request must use the POST method and match the RegEx, by default the RegEx is defined as follow:

...

Please read RFC1341 the Multipart section for more details, existing Struts Multipart parsers support only multipart/form-data content type. This option is available since Struts 2.3.11.

Disabling file upload support

You can alternatively disable the whole file upload mechanism defining a constant in struts.xml:

...

With this constant in place, Struts will ignore a Content-Type header and will treat each request as an ordinary http request. This option is available since Struts 2.3.11.