Issue
I want to limit each file's size to 20 MB, but it's only validating the size of all the input files. I have used the below properties to limit the file size. Could you please guide me on how I can restrict the size of each file?
spring: servlet: multipart: max-file-size: 20MB max-request-size: 20MB enabled: true
Solution
Here's what you need to do:
- Use custom annotation validation for all your input with different sizes.
- Set a default maximum size that Spring Boot can accept in your application.properties
To achieve step 1, lets say you have an Entity: Post
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import jakarta.persistence.*;
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String image;
@Transient
private MultipartFile imageContent;
// Getters and setters, constructors, etc.
}
With an interface, PostImageSize
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = ImageSizeValidation.class)
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface PostImageSize {
String message() default "Invalid image size";
long maxSize() default 5 * 1024 * 1024; // Default size is 5MB
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
and the class ImageSizeValidation mentioned in PostImageSize interface
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.web.multipart.MultipartFile;
public class ImageSizeValidation implements
ConstraintValidator<PostImageSize, MultipartFile> {
private long maxSize;
@Override
public void initialize(PostImageSize constraintAnnotation) {
this.maxSize = constraintAnnotation.maxSize();
}
@Override
public boolean isValid(MultipartFile multipartFile,
ConstraintValidatorContext cxt) {
return multipartFile == null || multipartFile.getSize() <=
maxSize;
}
}
Once you have set this up, using the annotation @PostImageSize in your entity will enable custom validation.
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String image;
@PostImageSize(message = "This image must not be more than 5MB")
private MultipartFile imageContent;
// Getters and setters, constructors, etc.
}
After this, you can customize the size according to the annotations you have and include in your entity which will enable it.
// Customizing to 1MB
@PostImageSize(message = "This image must not be more than 1MB", maxSize = 1024 * 1024)
private MultipartFile imageContent;
For step 2 in your application.properties
# Multipart settings
spring.servlet.multipart.max-file-size=256MB
spring.servlet.multipart.max-request-size=256MB
spring.servlet.multipart.enabled=true
Goodluck !!!
Answered By - Michael Chuks
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.