我有以下代码:
if (formItems != null && formItems.size() > 0) {
// iterates over form's fields
for (FileItem item : formItems) {
// 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);
session.setAttribute("image", fileName);
}
// processes only form fields
else {
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
session.setAttribute(fieldname, fieldvalue);
}
}
}
我想先检查一个文件是否附加。如果附加,则仅上传文件。我试过这个:
if(item==null)
但这行不通。如何检查文件是否附加?我有一个文件字段:
<input type="file" name="image"/>