Multiple File Upload example in Struts 2

File Upload is one of the common tasks of a web application and Struts 2 provides built-in feature for multiple files upload through FileUploadInterceptor. Interceptors are cross cutting concerns or common concerns like Logging; for more information on Interceptors please refer to Struts2 Interceptors

FileUploadInterceptor(for more information go to File upload interceptors) 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.

We will look into the implementation through a sample Struts 2 project where we will upload multiple files.

Please make sure that the following jars exist in the classpath

file upload example in struts

Let’s look at different parts of the application for uploading multiple files.

web.xml

 

multiple.jsp

 

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

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 10 MB by setting the value of struts.multipart.maxSize to 10*1024*1024.

FileUploadInterceptor default limit for single file size is 2 MB, so I am overriding it for uploadSingleFile action. I am using defaultStack with params maximumSize (10MB) and allowedTypes (text/plain,image/jpeg,image/png,image/gif,image/pjpeg) for fileUpload interceptor.

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

MultipleFileUploadAction.java

 

Notice that action class is implementing ServletContextAware interface that contains overridden 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.

The filePath parameter defined for uploadMultipleFile action, hence we have setFilePath() method in the above class. 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 file upload and params interceptors are File, file name and content type. If the file element name in request is “f” then these variables should be – File f, String fContentType and String fFileName. Since our single.jsp file element name is “file”, we have variables file, fileFileName and fileContentType with their getter and setters. Here I have used array for file and fileName because of multiple files wil be uploaded.

Now when we execute the file upload action then we get the response.

Run the application in server
file upload example in struts

If you do not select any file and click on Upload button then you will get error message
file upload example in struts

Now if you select files and click on Upload button and if the files successfully uploaded then you will get successful message otherwise you will get error message
file upload example in struts

Now files are successfully uploaded to the location : <Eclipse workspace>\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\struts2-file-upload\<uploaded files>

Thanks for your reading. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.