Java Multiple Files Upload Example
- Details
- Written by Nam Ha Minh
- Last Updated on 27 June 2019   |   Print Email
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:
Related Java File Upload Tutorials:
- Java File Upload Example with Servlet 3.0 API
- Java File Upload Example with Servlet, JSP and Apache Commons FileUpload
- Spring MVC File Upload Tutorial with Eclipse IDE
- Upload file with Struts
- Upload files to database (Servlet + JSP + MySQL)
- Upload Files to Database with Spring MVC and Hibernate
Other Java Servlet Tutorials:
- Java Servlet Quick Start for beginners (XML)
- Java Servlet for beginners (annotations)
- Handling HTML form data with Java Servlet
- Java File Download Servlet Example
Comments
I need code for downloading audio file from MySQL database that i uploaded.