【发布时间】:2015-05-18 23:23:55
【问题描述】:
目标:用户上传图片,验证器检查以确保它是用户上传的图片文件,如果不是图片则返回消息,如果是则不返回。 p>
问题:当点击上传按钮时,无论上传的文件是图片还是非图片,都会返回验证器消息。
重点领域: 在 Validator 类中,System.out.println(partValueContentType); 行已将内容类型写入控制台,例如。 image/jpeg,但是当它在 if 语句中进行测试时,它似乎根本没有评估内容类型。
String partValueContentType = part.getContentType();
System.out.println(partValueContentType);
if (!partValueContentType.equals("image/jpeg")
|| !partValueContentType.equals("image/jpg")
|| !partValueContentType.equals("image/gif")
|| !partValueContentType.equals("image/png"))
{
FacesMessage msg = new FacesMessage("File is not an image.",
"Acceptable image types (jpeg, jpg, gif, png)");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
这是怎么引起的,我该如何解决?
【问题讨论】:
-
喝杯好咖啡,再看一遍。将每个单独的条件替换为“真”或“假”。到底看起来还好吗?
-
出于调试目的,包含实际的
partValueContentType值。即FacesMessage msg = new FacesMessage("File is not an image.", "Acceptable image types (jpeg, jpg, gif, png) but got " + partValueContentType); -
@DilumRanatunga:你还需要一杯好咖啡。
-
大声告诉自己 if 语句中的子句是做什么的。什么时候是真的?
-
@BalusC - 哈哈,你是对的。那个结构看起来很奇怪,但我没有深入挖掘......
标签: java if-statement boolean-logic