[英]Unable to iterate over List<MultipartFile>
以下是我从网页上载多个文件的功能。 我的文件在MultipartFile列表-> files变量中。我无法遍历每个文件。 但是log.info(“ file” + files)打印文件[]。 这意味着文件返回“ []”。 这是什么意思? 我要去哪里错了?
public String addNodeAndDocNew(TreeType treeType, ObjectId parentNodeId, NodeType nodeType, String displayName, String attrList, List<MultipartFile> files, String userId,
String passwd) {
String result = "";
log.info("file"+ files);
log.info("userId=" + userId);
log.info("passwd=" + passwd);
log.info("nodeType=" + nodeType);
log.info("displayName=" + displayName);
if (attrList != null)
log.info("attributesList=" + attrList.toString());
ObjectId nodeId = null;
for(MultipartFile file : files)
{
log.info("Yess");
if (file.isEmpty()) {
log.info("Nothing attached, just adding node, not the doc");
nodeId = addNode(treeType, parentNodeId, nodeType, displayName, attrList, userId, passwd);
if (nodeId != null)
log.info("Just added node, no doc sent");
}
else {
log.info("file=" + file.getOriginalFilename());
nodeId = addNode(treeType, parentNodeId, nodeType, displayName, attrList, userId, passwd);
if (nodeId != null) {
docService.uploadDoc(nodeId, file, userId, passwd);
log.info("Added node, and doc");
} else {
log.info("addNode() returned null");
}
}
if (nodeId != null) {
log.info("addNodeAndDoc completed for node id : " + nodeId.toString());
result += nodeId.toString()+ "@@";
} else
return null;
}
log.info("Yess1");
return result;
}
但是log.info(files)打印了File []。
因此,您给定的数组为空。 这就是为什么您不能对其进行迭代! ;)
您使用的是增强的for
循环,而增强的for
循环则通过不执行该块来防止其尝试进行迭代的列表失败为空。 这就是这里正在发生的事情。
在可能遇到的情况下检查是否为empty
始终是一个好习惯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.