请求被拒绝,因为其大小为“Spring,tomcat

eni9jsuy  于 2023-04-30  发布在  Spring
关注(0)|答案(7)|浏览(467)

我试图用springboot做一个简单的上传应用程序,它工作正常,直到我尝试上传10 Mb+文件,我在屏幕上收到这个消息:

There was an unexpected error (type=Internal Server Error, status=500).
Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (14326061) exceeds the configured maximum (10485760)

我做了些调查,到现在为止都没什么用。我将离开这里下面的东西我已经尝试了。
把这段代码(以各种方式)在我的“应用程序。yml”

multipart: 
 maxFileSize: 51200KB
 maxRequestFile: 51200KB

我也在我的课堂上尝试过:

@Bean
public TomcatEmbeddedServletContainerFactory containerFactory() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
     factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
         ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
     });
     return factory;
}

还有一些奇怪的事情。如果我进入我的Tomcat网页。xml,multipart-config是:

<multipart-config>
      <!-- 50MB max -->
      <max-file-size>52428800</max-file-size>
      <max-request-size>52428800</max-request-size>
      <file-size-threshold>0</file-size-threshold>
    </multipart-config>

“这该死的东西在哪里?”.. configured maximum(10485760)”来自?(注:我使用的是netbeans 8。1和springboot 1。5)。
谢谢各位。(为英语s2抱歉)
既然问了,这是我的申请。YML

server:
      port: 9999
      context-path: /client
    logging:
      level:
        org.springframework.security: DEBUG
    endpoints:
      trace:
        sensitive: false

    spring:
        thymeleaf:
            cache: false
        multipart: 
          maxFileSize: 51200KB
          maxRequestFile: 51200KB  

    #################################################################################

    security:
      basic:
        enabled: false
      oauth2:
        client:
          client-id: acme2
          client-secret: acmesecret2
          access-token-uri: http://localhost:8080/oauth/token
          user-authorization-uri: http://localhost:8080/oauth/authorize
        resource:
          user-info-uri: http://localhost:8080/me
    #
eqfvzcg8

eqfvzcg81#

spring:
  http:
    multipart:
      enabled: true
      max-file-size: 50MB
      max-request-size: 50MB

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

参考here
希望它能起作用

pdtvr36n

pdtvr36n2#

以下是基于版本的方法,
第一名:

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

第二:

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

第三名:

multipart.enabled=true
multipart.max-file-size=100MB
multipart.max-request-size=100MB
cetgtptt

cetgtptt3#

对于SpringBoot 1.5.7到2.1。2需要在www.example中设置的属性 www.example.com 文件是:

spring.http.multipart.max-file-size=100MB
spring.http.multipart.max-request-size=100MB

另外,请确保您有 www.example.com 文件在“资源”文件夹中。

jchrr9hc

jchrr9hc4#

spring:
  servlet: 
    multipart: 
       enabled: true 
       file-size-threshold: 200KB   
       max-file-size:       500MB 
       max-request-size:    500MB
2ul0zpep

2ul0zpep5#

在SpringBoot 2中。6.3设置“Spring。http.multipart.max-file-size”不起作用。以下为我工作:

spring:
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB
kmpatx3s

kmpatx3s6#

用于配置CommonsMultipartResolver定义一个bean,bean名为MultipartFilter。DEFAULT_MULTIPART_RESOLVER_BEAN_NAME作为默认的spring Boot 的默认MultipartFilter查找具有默认bean名称的解析器。

@Bean(name = MultipartFilter.DEFAULT_MULTIPART_RESOLVER_BEAN_NAME)
protected MultipartResolver getMultipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(20971520);
    multipartResolver.setMaxInMemorySize(20971520);
    return multipartResolver;
}
f87krz0w

f87krz0w7#

我也有这个问题,我不知道为什么设置属性Spring。http.multipart.max-file-size=20MB和spring。http.multipart.max-request-size= 20 MB在www. example中 www.example.com 。要更改最大文件大小,我遵循了以下指南https://www.baeldung.com/spring-maxuploadsizeexceeded
所以我把它添加到我的主要类中:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver
      = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(20000000);
    return multipartResolver;
}

然后,为了处理MaxUploadSizeExceededException,我复制了

@ControllerAdvice
public class FileUploadExceptionAdvice {
     
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView handleMaxSizeException(
      MaxUploadSizeExceededException exc, 
      HttpServletRequest request,
      HttpServletResponse response) {
 
        ModelAndView modelAndView = new ModelAndView("file");
        modelAndView.getModel().put("message", "File too large!");
        return modelAndView;
    }
}

写了这个简单的文件HTML模板:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3 th:text="${message}"></h3>
</body>
</html>

添加此代码后,我在日志中看到MaxUploadSizeExceededException错误已处理,但在浏览器中仍有错误。解决方案是将这个添加到ww. example www.example.com

server.tomcat.max-swallow-size=60MB

如本教程所示:https://www.youtube.com/watch?v=ZZMcg6LHC2k

相关问题