Переглянути джерело

Fix 修复 undertow 新版本无法上传大文件问题

Yue 1 тиждень тому
батько
коміт
9f1564e250

+ 2 - 2
SERVER/VberAdminPlusV3/vber-admin/src/main/resources/application.yml

@@ -32,8 +32,8 @@ server:
     context-path: /
   # undertow 配置
   undertow:
-    # HTTP post内容的最大大小。当值为-1时,默认值为大小是无限的
-    max-http-post-size: -1
+    # HTTP post内容的最大大小
+    max-http-post-size: 1GB
     # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
     # 每块buffer的空间大小,越小的空间被利用越充分
     buffer-size: 512

+ 15 - 1
SERVER/VberAdminPlusV3/vber-common/vber-common-web/src/main/java/com/vber/common/web/config/UndertowConfig.java

@@ -1,11 +1,16 @@
 package com.vber.common.web.config;
 
 import com.vber.common.core.utils.SpringUtils;
+
+import io.undertow.UndertowOptions;
 import io.undertow.server.DefaultByteBufferPool;
 import io.undertow.server.handlers.DisallowedMethodsHandler;
 import io.undertow.util.HttpString;
 import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
+
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
 import org.springframework.boot.web.server.WebServerFactoryCustomizer;
 import org.springframework.core.task.VirtualThreadTaskExecutor;
@@ -18,6 +23,9 @@ import org.springframework.core.task.VirtualThreadTaskExecutor;
 @AutoConfiguration
 public class UndertowConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
 
+    @Autowired
+    private ServerProperties serverProperties;
+
     /**
      * 自定义 Undertow 配置
      * <p>
@@ -31,11 +39,17 @@ public class UndertowConfig implements WebServerFactoryCustomizer<UndertowServle
      */
     @Override
     public void customize(UndertowServletWebServerFactory factory) {
+        long bytes = serverProperties.getUndertow().getMaxHttpPostSize().toBytes();
+        factory.addBuilderCustomizers(builder -> {
+            builder.setServerOption(UndertowOptions.MULTIPART_MAX_ENTITY_SIZE, bytes);
+        });
+
         factory.addDeploymentInfoCustomizers(deploymentInfo -> {
             // 配置 WebSocket 部署信息,设置 WebSocket 使用的缓冲区池
             WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
             webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(true, 1024));
-            deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);
+            deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo",
+                    webSocketDeploymentInfo);
             // 如果启用了虚拟线程,配置 Undertow 使用虚拟线程池
             if (SpringUtils.isVirtual()) {
                 // 创建虚拟线程池,线程池前缀为 "undertow-"