首頁 > 軟體

SpringBoot 上傳檔案判空以及格式檢驗流程

2022-03-24 13:01:18

基於jsr303 通過自定義註解實現,實現思路:

存在一些瑕疵,後續補充完善。

加入依賴

部分版本已不預設自動引入該依賴,選擇手動引入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

建立自定義註解以及實現類

目錄結構:

  • FileNotEmpty 自定義註解
  • FileNotEmptyValidator 單檔案校驗
  • FilesNotEmptyValidator 多檔案校驗
/**
 * jsr303 檔案格式校驗註解
 *
 * @author maofs
 * @version 1.0
 * @date 2021 -11-29 10:16:03
 */
@Documented
@Constraint(
        validatedBy = {FileNotEmptyValidator.class, FilesNotEmptyValidator.class}
)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FileNotEmpty {
    /**
     * Message string.
     *
     * @return the string
     */
    String message() default "檔案格式不正確";
    /**
     * 校驗組
     *
     * @return the class [ ]
     */
    Class<?>[] groups() default {};
    /**
     * Payload class [ ].
     *
     * @return the class [ ]
     */
    Class<? extends Payload>[] payload() default {};
    /**
     * 需要校驗的格式陣列
     *
     * @return the string [ ]
     */
    String[] format() default {};
    /**
     * 是否必填 為false時檔案為空則不校驗格式,不為空則校驗格式
     * 為true時檔案不能為空且需要驗證格式
     *
     * @return the boolean
     */
    boolean required() default true;
/**
 * 單檔案校驗
 *
 * @author maofs
 * @version 1.0
 * @date 2021 -11-29 10:16:03
 */
public class FileNotEmptyValidator implements ConstraintValidator<FileNotEmpty, MultipartFile> {
    private Set<String> formatSet = new HashSet<>();
    private boolean required;
    @Override
    public void initialize(FileNotEmpty constraintAnnotation) {
        String[] format = constraintAnnotation.format();
        this.formatSet = new HashSet<>(Arrays.asList(format));
        this.required = constraintAnnotation.required();
    }
    @Override
    public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext constraintValidatorContext) {
        if (multipartFile == null || multipartFile.isEmpty()) {
            return !required;
        }
        String originalFilename = multipartFile.getOriginalFilename();
        assert originalFilename != null;
        String type = originalFilename.substring(originalFilename.lastIndexOf('.') + 1).toLowerCase();
        if (!formatSet.isEmpty()) {
            return formatSet.contains(type);
        }
        return true;
    }
}
/**
 *  多檔案校驗
 *
 * @author maofs
 * @version 1.0
 * @date 2021 -11-29 10:16:03
 */
public class FilesNotEmptyValidator implements ConstraintValidator<FileNotEmpty, MultipartFile[]> {
    private Set<String> formatSet = new HashSet<>();
    private boolean required;
    @Override
    public void initialize(FileNotEmpty constraintAnnotation) {
        String[] format = constraintAnnotation.format();
        this.formatSet = new HashSet<>(Arrays.asList(format));
        this.required = constraintAnnotation.required();
    }
    @Override
    public boolean isValid(MultipartFile[] multipartFiles, ConstraintValidatorContext constraintValidatorContext) {
        if (multipartFiles == null || multipartFiles.length == 0) {
            return !required;
        }
        for (MultipartFile file : multipartFiles) {
            String originalFilename = file.getOriginalFilename();
            assert originalFilename != null;
            String type = originalFilename.substring(originalFilename.lastIndexOf('.') + 1).toLowerCase();
            if (formatSet.isEmpty() || !formatSet.contains(type)) {
                return false;
            }
        }
        return true;
    }
}

全域性例外處理

/**
 * 統一例外處理
 *
 * @author maofs
 * @version 1.0
 * @date 2021 -11-29 10:16:03
 */
@ControllerAdvice
public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
       
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result<String> handle(Exception e) {
        logger.error(e.getMessage());
        StringBuilder stringBuilder = new StringBuilder();             
            //jsr303異常
          if (e instanceof ConstraintViolationException) {
            ConstraintViolationException ex = (ConstraintViolationException)e;
            Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
            for (ConstraintViolation<?> constraintViolation : constraintViolations) {
                stringBuilder.append(constraintViolation.getMessageTemplate());
            }
        } else if (e instanceof BindException) {
            BindException bindException = (BindException)e;
            stringBuilder.append(bindException.getFieldErrors()
                .stream()
                .map(FieldError::getDefaultMessage)
                .collect(Collectors.joining(",")));
        } else {
            stringBuilder.append("未知錯誤:").append("請聯絡後臺運維人員檢查處理!");           
        }
        return  ResultUtil.fail(stringBuilder.toString());
    }    
}

使用範例

/**
 * 檔案上傳範例介面
 *
 * @author maofs
 * @version 1.0
 * @date 2021 -11-19 16:08:26
 */
@RestController
@Validated
@RequestMapping("/annex")
public class AnnexController {
 @Resource
 private IAnnexService annexService;
   /**
     * 檔案上傳範例1
     *
     * @param uploadDTO the upload dto
     * @return the result
     */
    @PostMapping(value = "/upload1")
    public Result<String> upload(@Valid AnnexUploadDTO uploadDTO) {
        return Boolean.TRUE.equals(annexService.upload(uploadDTO)) ? ResultUtil.success() : ResultUtil.fail();        
    }
    
   /**
     * 檔案上傳範例2
     *
     * @param number      專案編號
     * @param pictureFile 圖片檔案
     * @param annexFile   附件檔案
     * @return result result
     */
    @PostMapping(value = "/upload2")
    public Result<String> upload(@NotBlank(@FileNotEmpty(format = {"png", "jpg"}, message = "圖片為png/jpg格式", required = false)
                                         MultipartFile pictureFile, @FileNotEmpty(format = {"doc", "docx", "xls", "xlsx"}, message = "附件為doc/docx/xls/xlsx格式", required = false)
                                         MultipartFile annexFile) {       
        return Boolean.TRUE.equals(annexService.upload( pictureFile, annexFile)) ? ResultUtil.success() : ResultUtil.fail();
    }
    
	@Data
	static class AnnexUploadDTO{ 
	    @FileNotEmpty(format = {"pdf","doc","zip"}, message = "檔案為pdf/doc/zip格式")
	    private MultipartFile[] file;
    }
}

結果展示

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com