I want to check file extension on upload usin ConstraintValidator. If the file extension is not as specified in the annotation, then the user should get common constraint validation error response. But when it happens this exception occurs
java.io.FileNotFoundException: C:\Users\Tensky\AppData\Local\Temp\tomcat.4393944258717178584.8443\work\Tomcat\localhost\ROOT\upload_a19ba701_88a1_4eab_88b7_eede3a273fe0_00000011.tmp
And user get Bad Request page
My classes:
Annotation @UploadFileTypes
:
@Documented
@Constraint(validatedBy = UploadFileTypesValidator.class)
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface UploadFileTypes {
String message() default "Example message";
UploadFileType[] allowedTypes() default {UploadFileType.AUDIO, UploadFileType.VIDEO, UploadFileType.DOCUMENT, UploadFileType.IMAGE};
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Documented
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface List{
UploadFileTypes[] value();
}
}
Class UploadFileTypesValidator:
public class UploadFileTypesValidator implements ConstraintValidator<UploadFileTypes, MultipartFile> {
@Override
public void initialize(UploadFileTypes constraintAnnotation) {
}
@Override
public boolean isValid(MultipartFile value, ConstraintValidatorContext context) {
return false; //<- always exception
}
}
And controller (never reach, becouse isValid always return false:
@RequestMapping(path = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity addProperty(@Valid PropertyCreateRequest request) {
return ResponseEntity.ok(null);
}
Why does this exception occur and how to avoid it?