这里我想避免基于使用选择控件选择的作者姓名重复文件名。但是在这里我没有在文件保存部分中获得作者名称null。
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
String author=null;
FileItem item = (FileItem) iterator.next();
//form fileds
if(item.isFormField()){
String fieldName = item.getFieldName();
if(fieldName!=null&&fieldName.equals("author"){
author=item.getString();
}
//files
if (!item.isFormField()) {
String fileName = item.getName();
File path = new File("D:/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/"+author+"_"+ fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}