这个问题已经在这里有了答案: > Upload multiple files at once to a Struts2 @Action 2个
如何在任何位置上传多个文件.我的问题是我要选择多个文件,但是当我单击“上传”按钮时,只有最后一个文件以重命名名称上传,而重命名名称是所有文件名,并以逗号开头,如下所示(file1,file2,flie3)
这是代码
File saveFile = null;
String tempPath = System.getProperty("java.io.tmpdir");
saveFile = new File(tempPath + File.separator + fileUploadFileName);
FileUtils.copyFile(fileUpload, saveFile);
解决方法:
通过使用Apache commons fileupload FileItem,示例代码将如下所示
try {
// parses the request's content to extract file data
List formItems = upload.parseRequest(request);
Iterator iter = formItems.iterator();
// iterates over form's fields
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// processes only fields that are not form fields
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// saves the file on disk
item.write(storeFile);
}
}
request.setAttribute("message", "Upload has been done successfully!");
} catch (Exception ex) {
request.setAttribute("message", "There was an error: " + ex.getMessage());
ex.printStackTrace();
}
从Multi File Upload下载MultipleFilesUpload.zip.有关更多详细信息,请参考此Upload: