In the tutorial File Upload Servlet with Apache Common File Upload API, the sample application is designed for single file upload only. This article shows how to modify that application in order to handle multiple files upload, as many files as needed. It’s easy to tweak the application to be able to upload 5 files at once, with some little changes:

-          Update upload form in the upload.jsppage: Create 5 file input components:

Select file #1: <input type="file" size="50" />
Select file #2: <input type="file" size="50" />
Select file #3: <input type="file" size="50" />
Select file #4: <input type="file" size="50" />
Select file #5: <input type="file" size="50" /> 

-          Increase value of the constant REQUEST_SIZE: because there are more files to upload, the request size should be bigger. Suppose that we allow maximum 50 MB for request size:

private static final int REQUEST_SIZE = 1024 * 1024 * 50; // 50MB

So source code of the upload.jsp file would change to:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
    <center>
        <h1>Multiple Files Upload</h1>
        <form method="post" action="UploadServlet"
            enctype="multipart/form-data">
            Select file #1: <input type="file" size="50" /><br />
            Select file #2: <input type="file" size="50" /><br />
            Select file #3: <input type="file" size="50" /><br />
            Select file #4: <input type="file" size="50" /><br />
            Select file #5: <input type="file" size="50" /><br />
            <br /> <input type="submit" value="Upload" />
        </form>
    </center>
</body>
</html>

And the rest of the application won’t change, except the increment of request size value.

Here is the screenshot of the updated upload form:

multiple files upload form

 

Related Java File Upload Tutorials:

 

Other Java Servlet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (UploadServlet.java)UploadServlet.java[source code]2 kB

Add comment

   


Comments 

#8Pankaj2020-07-06 00:41
Hello sir, Please help me out

I need code for downloading audio file from MySQL database that i uploaded.
Quote
#7Priya2016-05-03 02:42
Thank you for your examples and keep me in touch because iam new to angularjs in this field.
Quote
#6Malvika Pandya2015-12-12 06:39
where to store all files in this example?
Quote
#5Gane2015-08-25 01:19
This is useless. Not working and no one to clarify
Quote
#4dan2015-06-17 02:24
this is not working maybe since code to trying to open the same folder on each iteration of the loop.
Quote