简体   繁体   English

使用 spring MultipartFile 验证文件大小

[英]File size validation with spring MultipartFile

I need to do a validation on file size in spring MultipartFile in a spring boot application.我需要在 Spring Boot 应用程序中对 Spring MultipartFile 中的文件大小进行验证。 For example I want to restrict files upload more than 10MB.例如我想限制文件上传超过 10MB。 So i used below in application.yml,所以我在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我注意到的问题是,当请求命中时,它看起来像是上传了整个 10MB,然后只有它抛出异常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?我正在考虑一种更有效的方法,因为在没有请求保留整个 10MB 的情况下上传并在它可以进行验证之前抛出异常可能是查看要上传的文件的元数据并抛出异常?

You can make use of Content-Length header attribute.您可以使用Content-Length标头属性。 Basically, it is the number of bytes of data in the body of the request or response.基本上,它是请求或响应正文中的数据字节数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM