undertow容器处理上传文件过大异常

什么是undertow

undertow是RedHat(红帽公司)的开源产品,采用java开发,是一款灵活、高性能的web服务器,提供了基于NIO的阻塞/非阻塞式APIs,也是WildFly(原名JBoss AS - JBoss Application Server)的默认web容器。

undertow是springboot默认支持的三种servlet容器之一。
tomcat、jetty、undertow

官方网站
https://undertow.io/index.html

业务场景:
测试文件上传的时候遇到如下:“Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: io.undertow.server.handlers.form.MultiPartParserDefinition$FileTooLargeException: UT000054: The maximum size 1048576 for an individual file in a multipart request was exceeded”
开发环境:
springBoot2.3.2.RELEASE 容器undertow
问题原因:
容器undertow在上传文件时存在一个容易忽略的配置问题:
由于springboot具有几个版本,不同版本对于文件上传最大限制的配置也有所不同。
所以要注意springboot本身的版本,不然会一直报错
在springboot1.3版本中:
multipart.maxFileSize
在springboot1.4与springboot1.5版本中:
properties:

spring.http.multipart.max-file-size:10MB
spring.http.multipart.max-request-size:50MB

yml:

spring:
  http:
   multipart:
    maxFileSize: 10Mb
    maxRequestSize: 50Mb

在springboot2.0版本中:
properties:

spring.servlet.multipart.enabled=true # 是否支持多部分上传。
spring.servlet.multipart.max-file-size:10MB # 最大支持文件上传的大小
spring.servlet.multipart.max-request-size:50MB # 支持请求最大文件上传的大小

yml:

spring:
  servlet:
   multipart:
    maxFileSize: 10Mb
    maxRequestSize: 50Mb

说明:
FileTooLargeException异常判断就是对undertow容器的支持,SpringBoot不会被系统异常拦截机制捕获到,这应该是容器的问题,建议用tomcat。

解决办法:
参考网上的解决问题的办法大致有以下几种:

第一种:使用spring-boot-starter-web的内置Tomcat

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

AI回答:spring-boot-starter-web如何内置Tomcat?
在Spring Boot中,spring-boot-starter-web 是一个模块化依赖(starter),它为开发基于Web的应用程序提供了所有必需的依赖项。当你将 spring-boot-starter-web 添加到项目的 pom.xml 文件时,它会间接引入嵌入式的Tomcat容器作为Servlet容器。通过继承自 spring-boot-starterspring-boot-dependencies 的父POM来管理这些底层依赖版本的。版本是通过父POM中的 <properties> 标签进行集中管理的,因此实际使用的Tomcat版本会根据你使用的Spring Boot starter parent版本动态确定。

第二种:使用全局捕获异常(MaxUploadSizeExceededException)

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandlerResolver {
    /**
     * @Descript   统一处理文件过大问题.
     */
 @ExceptionHandler(MaxUploadSizeExceededException.class)
    public R handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        log.error("上传文件过大 ex={}", e);
        return retMsgCode("上传文件过大", 9999;
    }
}

第三种:前端进行控制

但是这种方法只是治标不治本,没有从根本上解决问题,而且不符合解决问题的一般步骤。

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要配置Undertow的最大连接数,需要将默认的Tomcat容器改为Undertow,并设置相应的参数。具体的配置步骤如下: 1. 在配置文件中排除Spring Boot的Tomcat容器依赖。在pom.xml文件中添加以下代码: ```xml <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> ``` 2. 创建一个配置类,比如TomcatConfig,用于配置Undertow容器。在配置类中添加以下代码: ```java @Configuration public class TomcatConfig { @Bean public ConfigurableServletWebServerFactory webServerFactory() { UndertowServletWebServerFactory undertowFactory = new UndertowServletWebServerFactory(); undertowFactory.addBuilderCustomizers(builder -> { builder.setIoThreads(200); // 设置最大IO线程数 builder.setWorkerThreads(2000); // 设置最大工作线程数 }); undertowFactory.setPort(8005); // 设置端口号 undertowFactory.setContextPath("/api-g"); // 设置上下文路径 return undertowFactory; } } ``` 通过以上配置,可以将默认的Tomcat容器改为Undertow,并设置最大连接数。在Undertow的配置中,通过addBuilderCustomizers方法可以设置最大IO线程数和最大工作线程数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [7 种提升 SpringBoot 吞吐量神技!](https://blog.csdn.net/weixin_46768610/article/details/124335618)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot 性能太差?试试这几招](https://blog.csdn.net/m0_69305074/article/details/126904439)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值