How to put validations for MultipartFile in Spring Boot?


Using below code we can put validations for MultipartFile in Spring Boot:

private void validateExtension(MultipartFile file) {
    String extension = FilenameUtils.getExtension(file.getOriginalFilename());
    if (!"png".equals(extension) && !"jpeg".equals(extension) && !"jpg".equals(extension)) {
        throw new InvalidFileExtensionException("Only jpg/jpeg and png files are accepted");

private void validateFileSize(MultipartFile file){
    if (file.getSize() >= MAXIMUM_FILE_SIZE_ALLOWED) {
        throw new BusinessException("File size cannot be greater than 5 Mb");


Related #tags:     #java     #spring-boot     #spring    

Related questions

Looking for change? Click below to prepare for technical interview.

Technical Interview Questions