Questions #Tagged [spring]

Questions: 1

How to put validations for MultipartFile in Spring Boot?

Using below code we can put validations for MultipartFile in Spring Boot:<br> <pre><xmp>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"); } }</xmp></pre>

 

#java     #spring-boot     #spring