In my JSP
I have a form where the user can upload a file.
<form method="post" action="linkToAction" role="form" enctype="multipart/form-data">
<input name="files" type="file" class="form-control-file" multiple="true" >
</form>
Reading this answer, in my servlet
I get the files uploaded:
List<Part> fileParts = request.getParts().stream().filter(part -> "files".equals(part.getName())).collect(Collectors.toList());
If the user doesn't upload anything, I would expect that the List<Part> fileParts
would be empty! However, it is not as the above statement adds in the list fileParts
, an empty Part
. If the user uploads 1 file, the above statement adds again 1 valid Part
object.
One quick solution that I thought, is to check the length of the filename:
for (Part filePart : fileParts) {
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); /
if (fileName.length() != 0) {
//do my job here
}
}
However, why is this happening?