Harshana
Harshana

Reputation: 7647

File size validation with spring MultipartFile

I need to do a validation on file size in spring MultipartFile in a spring boot application. For example I want to restrict files upload more than 10MB. So i used below in application.yml,

servlet:
        multipart:
            max-file-size: 10MB
            max-request-size: 10MB

The problem I noticed it, when a request hits looks like it upload the whole 10MB and then only it throws the exception java.lang.IllegalStateException: io.undertow.server.RequestTooBigException

I am thinking a more efficient way as in with out request holding for whole 10MB is uploaded and throwing the exception before that it spring could do the validation may be look at meta data of file going to upload and throw the exception?

Upvotes: 0

Views: 1049

Answers (1)

Amritesh Khare
Amritesh Khare

Reputation: 29

You can make use of Content-Length header attribute. Basically, it is the number of bytes of data in the body of the request or response.

Upvotes: 0

Related Questions