Why can't upload three files with FileUpload ?
- by Valter Henrique
Hi everyone,
i'm trying to upload three images to my server, is working, but upload always the last file selected by the user, not the three selected.
Here's my code:
protected void doPost(HttpServletRequest request, HttpServletResponse response){
boolean multipart = ServletFileUpload.isMultipartContent(request);
if (multipart) {
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setSizeThreshold(5 * 1024 * 1024); //5 MB
fileItemFactory.setRepository(tmpDir);
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
File file = new File(dir, generateNewName());
item.write(file);
}
} catch (FileUploadException ex) {
} catch (Exception ex) {
}
}
}
--
UPDATE:
<html>
<head>
<title>Upload</title>
</head>
<body>
<form action="Upload" method="post" enctype="multipart/form-data">
<input type="file" name="file1" />
<br />
<input type="file" name="file2" />
<br />
<input type="file" name="file3" />
<br />
<input type="submit" value="Enviar" />
</form>
</body>
</html>
Best regards,
Valter Henrique.