Struts 2 File Upload Example

Welcome to Struts 2 file upload example. File Upload is one of the common tasks of a web application and Struts 2 provides built-in feature for single and multiple file upload through FileUploadInterceptor.

Struts 2 File Upload

Struts 2 FileUploadInterceptor interceptor is configured in the struts-default package that we usually extend in Struts 2 package configuration.

FileUploadInterceptor also provide options to set the maximum file size limit, allowed file types and extensions that can be uploaded to the server.

Struts 2 provide option to configure the maximum file size limit through struts.multipart.maxSize variable. This comes handy to set the limit to upload files incase of multiple files uploading in a single request.

FileUploadInterceptor intercepts the request with enctype as “multipart/form-data” and automatically saves the file in the temp directory and provide useful reference to File, file name and file content type to action classes to save the file at specified location.

Struts 2 File Upload Example

We will look into the implementation through a sample Struts 2 project where we will upload single file as well as multiple files to server. Our final project structure looks like below image.

Struts2-File-Upload-Project-346x450

Struts 2 File Upload Configuration

Let’s look at different parts of the application for uploading a single file.

web.xml

Deployment descriptor is self explanatory, just adding Struts 2 filter to intercept the client requests.

pom.xml

Maven configuration file is also easy to understand and includes only struts-core dependency.

UploadFile.jsp

The important points to note in UploadFile.jsp are enctype (multipart/form-data), file element name (file) and action (UploadFile).

Before we move to action class implementation, let’s look at the struts.xml configuration file.

struts.xml

Notice that I have set the maximum file size limit in a single request to 100 MB by setting the value of struts.multipart.maxSize to 100*1024*1024.

FileUploadInterceptor default limit for single file size is 2 MB, so I am overriding it for UploadFile action. I am using defaultStack with params maximumSize (10MB) and allowedTypes (txt, jpg) for fileUpload interceptor.

Now we are ready to look into the action class implementation.

UploadFileAction.java

Notice that action class is implementing ServletContextAware interface that contains single method setServletContext(). This is done to get the ServletContext reference in action class and use it to get the web application root directory. This will be used in saving the file from temp directory to another directory inside web application.

Did you noticed filesPath parameter defined for UploadFile action, that’s why we have setFilesPath() method. This method is used by Struts 2 params interceptor to read the params configured in struts.xml file and invoke it’s setter method.

The three variables that are set by fileUpload and params interceptors are File, file name and content type. If the file element name in request is “pic” then these variables should be – File pic, String picContentType and String picFileName. Since our UploadFile.jsp file element name is “file”, we have variables file, fileFileName and fileContentType with their getter and setters.

FilesUtil is utility class that is used to save the file from temp directory to another directory in the web application.

FileUtil.java

The response JSP page after successful file upload code is:

UploadFileSuccess.jsp

Notice the use of fileFileName variable from action class is used in response JSP page.

Our implementation for single file upload is ready, when we execute the application we get following response pages.

Struts2-File-Upload-Request-450x172

Struts2-File-Upload-Request-450x172

We get following logs in server logs.

Above logs confirm that fileUpload interceptor uploads file in temp directory and rename it with .tmp extension. The same file is copied from there to our specified location.

Struts 2 Multiple File Upload

If you understood how single file upload works, then multiple file upload is very easy. FileUploadInterceptor saves all the files in request to temp directory and we can use Array or List to get their reference in action classes.

UploadMultipleFile.jsp

Notice that all the file elements name are same.

UploadMultipleFileAction.java

Action class is similar as single upload, except that the variables are now array or list to hold multiple values. We are iterating the array and saving each file to specific directory.

UploadMultipleFileSuccess.jsp

To configure them, just add below action elements in existing struts.xml file.

Now when we execute multiple file upload action, we get following response pages.

Struts2-Multiple-File-Upload-Request-450x221

We get following log snippet in the server log file.

That’s all for Struts 2 file upload example, you can download the complete project from below link and learn more about it.

By admin

Leave a Reply

%d bloggers like this: