Reputation: 413
I know how to save it when only one file is uploaded in servlet.
HTML
<form action="storeArticle" method="post" enctype="multipart/form-data">
<input type="file" name="file">
...
</form>
Servlet can save the uploaded file as follows:
Part part = request.getPart("file");
File file = new File(filePath);
try (InputStream inputStream= part.getInputStream()) { // save uploaded file
Files.copy(inputStream, file.toPath());
}
For example, in https://stackoverflow.com/questions/ask, user can choose uploading multiple images by clicking the only one image icon.But when multiple files are uploaded one time, how can servlet save these uploaded files?
HTML
<input type="file" name="file[]" multiple >
Upvotes: 0
Views: 903
Reputation: 454
<form action="storeArticle" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="file" name="file2">
<input type="file" name="file3">
</form>
The servlet does this
Part part = request.getPart("file");
File file = new File(filePath);
try (InputStream inputStream= part.getInputStream()) { // save uploaded file
Files.copy(inputStream, file.toPath());
}
Part part = request.getPart("file2");
File file = new File(filePath);
try (InputStream inputStream= part.getInputStream()) { // save uploaded file
Files.copy(inputStream, file.toPath());
}
Part part = request.getPart("file3");
File file = new File(filePath);
try (InputStream inputStream= part.getInputStream()) { // save uploaded file
Files.copy(inputStream, file.toPath());
}
Upvotes: 1