Upload multiple files using single browse button in Struts2

This tutorial shows how to upload multiple files using single browse button in Struts2. There is another tutorial on Multiple Files Upload in Struts2 which uses multiple browse buttons for multiple file upload but this tutorial uses only one button for uploading multiple file upload in Struts2. We need to use just attribute multiple=”multiple” with <s:file/> tag for uploading multiple files using the single browse button.

I won’t explain much details in this tutorial but if you need more explaination then you can go through another tutorial Multiple Files Upload in Struts2.

Prerequisites

Eclipse
Tomcat v7
JDK 1.6
Struts2 jars

file upload example in struts

Now we will see step by step how to upload multiple files below.

1. Create Dynamic Web Project in Eclipse.

2. Put the required jar files into the lib directory under WEB-INF folder.

3. Create a index.jsp file under WebContent folder. The index.jsp file contains the following code

 

4. Modify deployment descriptor file – web.xml, as shown below

 

5. Now create struts.xml file with the following source code and put it under src directory.

 

6. Create Controller or Action file MultipleFileUploadAction.java with the following source code.

 

When application runs and home page opens in browser

multiple files upload using single browse button in struts2
When you submit the Upload Files button without selecting any file using the browse button

multiple files upload using single browse button in struts2
When file(s) is/are selected using the browse button and Upload Files button is submitted

multiple files upload using single browse button in struts2
You will get the following output in console once files are successfully uploaded

 

Thank you for reading. Please do not forget to leave a comment if you have any query.

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.