在之前的《如何使用 Apache Commons + Servlet + JSP 实现文件上传》一文中已经演示了如何使用 Java 进行单文件的上传。本文将介绍如何实现多文件上传功能,其实多文件上传非常简单,只需要在之前的代码基础上做一点点修改即可。
更新 upload.jsp 文件,让它支持上传 5 个文件:
Select file #1:
Select file #2:
Select file #3:
Select file #4:
Select file #5:
增加常量 REQUEST_SIZE 的值,因为现在需要上传 5 个文件,因此请求的文件大小需要更大,假设最大总共上传 50 MB 的文件:
private static final int REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
完整的 upload.jsp 如下:
pageEncoding="ISO-8859-1"%>
File UploadMultiple Files Upload
enctype="multipart/form-data">
Select file #1:
Select file #2:
Select file #3:
Select file #4:
Select file #5:
应用程序的其它部分都不用改,除了 REQUEST_SIZE。
下面是上传页面运行后的效果: