gpt4 book ai didi

spring-mvc - 如何使用 Spring MVC 显示有关上传的多部分文件的验证错误

转载 作者:行者123 更新时间:2023-12-01 09:32:54 25 4
gpt4 key购买 nike

我有一个带有文件上传表单Spring MVC应用程序。

如果上传的内容无效(例如内容不是图像等),我希望能够向用户显示验证错误

但是,根据定义,发布的类型是:MultipartFile (multipart/form-data),因此我不能拥有 @ModelAttribute在我的表格中,为了使用 BindingResult ,看来我确实需要 @ModelAttribute就在 BindingResult 之前.

我的问题是,当我只有 MultipartFile 时,向用户显示验证错误的最合适方式是什么? ?我当然可以手动将模型属性添加到模型中,但我确信有更好的方法。

最佳答案

如果您使用的是 Java Bean Validation (JSR 303),您可以制作验证内容类型的注释。请参阅下面的代码。

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
/**
* The annotated element must have specified content type.
*
* Supported types are:
* <ul>
* <li><code>MultipartFile</code></li>
* </ul>
*
* @author Michal Kreuzman
*/
@Documented
@Retention(RUNTIME)
@Constraint(validatedBy = {ContentTypeMultipartFileValidator.class})
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
public @interface ContentType {

String message() default "{com.kreuzman.ContentType.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

/**
* Specify accepted content types.
*
* Content type example :
* <ul>
* <li>application/pdf - accepts PDF documents only</li>
* <li>application/msword - accepts MS Word documents only</li>
* <li>images/png - accepts PNG images only</li>
* </ul>
*
* @return accepted content types
*/
String[] value();
}

/**
* Validator of content type. This is simple and not complete implementation
* of content type validating. It's based just on <code>String</code> equalsIgnoreCase
* method.
*
* @author Michal Kreuzman
*/
public class ContentTypeMultipartFileValidator implements ConstraintValidator<ContentType, MultipartFile> {

private String[] acceptedContentTypes;

@Override
public void initialize(ContentType constraintAnnotation) {
this.acceptedContentTypes = constraintAnnotation.value();
}

@Override
public boolean isValid(MultipartFile value, ConstraintValidatorContext context) {
if (value == null || value.isEmpty())
return true;

return ContentTypeMultipartFileValidator.acceptContentType(value.getContentType(), acceptedContentTypes);
}

private static boolean acceptContentType(String contentType, String[] acceptedContentTypes) {
for (String accept : acceptedContentTypes) {
// TODO this should be done more clever to accept all possible content types
if (contentType.equalsIgnoreCase(accept)) {
return true;
}
}

return false;
}
}

public class MyModelAttribute {

@ContentType("application/pdf")
private MultipartFile file;

public MultipartFile getFile() {
return file;
}

public void setFile(MultipartFile file) {
this.file = file;
}
}
@RequestMapping(method = RequestMethod.POST)
public String processUploadWithModelAttribute(@ModelAttribute("myModelAttribute") @Validated final MyModelAttribute myModelAttribute, final BindingResult result, final Model model) throws IOException {
if (result.hasErrors()) {
// Error handling
return "fileupload";
}

return "fileupload";
}

关于spring-mvc - 如何使用 Spring MVC 显示有关上传的多部分文件的验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13083036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com