I am using a Java Servlet to handle an html form and it includes a file input element:
<input type="file" id="fileUpload" name="file" multiple />
I used the example code in this excellent answer to process multiple files at once. The code I'm using is:
List<Part> fileParts = req.getParts().stream().filter(part -> "file".equals(part.getName())).collect(Collectors.toList()); // Retrieves <input type="file" name="file" multiple="true">
for (Part filePart : fileParts) {
String fileName = filePart.getSubmittedFileName();
InputStream fileContent = filePart.getInputStream();
// Do stuff here
}
This code works great. My problem is, when I don't attach anything, my code still thinks fileParts has a Part object in it. Doing some debugging, the Part object does seem to be there, but of course there is no InputStream or SubmittedFileName to get, since I didn't upload any files. Why is this? I am new to lambda functions and collections, but it seems like this "fileParts" Collection should be empty when I don't select any files for upload.