简体   繁体   English

我如何验证某人是否已上传文件

[英]How can i do a validate if a person has uploaded a file

I am getting this error message. 我收到此错误消息。

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: byte[]. javax.validation.UnexpectedTypeException:HV000030:找不到类型为byte []的验证器。

The code is 该代码是

@NotBlank(message = DocumentForm.NOT_BLANK_MESSAGE)
private byte[] file;

How can i do a validate if a person has uploaded a file 我如何验证某人是否已上传文件

I don't know what annotations are available for a byte array, if any. 我不知道哪些注释可用于字节数组(如果有)。 However, you could implement your own validator without much trouble. 但是,您可以轻松实现自己的验证器。

Something like: 就像是:

public class ModelWithFileFieldValidator implements Validator {

    public boolean supports(Class clazz) {
        return ModelWithFileField.class.equals(clazz);
    }

    public void validate(Object obj, Errors e) {
        byte[] field = ((ModelWithFileField)obj).getBytes();
        if (field == null || field.length == 0) {
            e.rejectValue("file", "empty");
        }
    }
}

@NotBlank is for Strings. @NotBlank用于字符串。 It's also not part of the standard for that matter. 它也不是该标准的一部分。 You probably want to go with @NotNull . 您可能想使用@NotNull It only checks if a file is there, though, not if it's empty. 但是,它仅检查文件是否存在,而不是是否为空。 If that's necessary as well you can use @Size(min = 1) . 如果同样有必要,您可以使用@Size(min = 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM