简体   繁体   English

Spring Boot - 多部分文件最大上传大小异常

[英]Spring Boot - Multipart file maximum upload size exception

I'm developing a endpoint with Spring Boot 2.1.3<\/strong> (using the standard Tomcat embedded web server) to upload an image and I want to limit the size of the multipart upload.我正在使用 Spring Boot 2.1.3<\/strong> (使用标准的 Tomcat 嵌入式 Web 服务器)开发一个端点来上传图像,并且我想限制分段上传的大小。 I can do this easily with the properties:我可以使用以下属性轻松完成此操作:

spring:
    servlet:
        multipart:
            max-file-size: 2MB
            max-request-size: 2MB

A bit late.有点晚了。 You need set in application.properties or application.yml server.tomcat.max-swallow-size=-1 With it tomcat will not interferred in the upload request and the operation will be handle fully by spring and your exception controller will work perfectly.您需要在 application.properties 或 application.yml server.tomcat.max-swallow-size=-1有了它,tomcat 将不会干扰上传请求,并且操作将由 spring 完全处理,您的异常控制器将完美运行。 References 参考

Please add this in your application.properties :请在您的application.properties添加:

spring.servlet.multipart.max-file-size=50MB
spring.servlet.multipart.max-request-size=50MB

You can change the size as you need in the above config您可以在上面的配置中根据需要更改大小

Was having a similar problem.遇到了类似的问题。 Tomcat kept complaining that the request was exceeding the configured maximum (2097152), even though I had the default settings in place in the global web.xml: Tomcat 一直抱怨请求超出了配置的最大值 (2097152),即使我在全局 web.xml 中有默认设置:

      <max-file-size>52428800</max-file-size>
      <max-request-size>52428800</max-request-size>

Finally figured out: the servlet class I am developing extends a servlet class that has the @MultipartConfig annotation, but my local class did not have the @MultipartConfig annotation.终于想通了:我正在开发的servlet类扩展了一个带有@MultipartConfig注解的servlet类,但是我的本地类没有@MultipartConfig注解。 So the request was not being treated as multi-part, which was tripping this error.所以请求没有被视为多部分,这导致了这个错误。

The code that was throwing the error is in the super class at these lines:引发错误的代码位于这些行的超类中:

//Search through the parts for an uploaded file
for (Part part : request.getParts()) {

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

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