From 829f357bae7083a8793762ce41105adb63982688 Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Mon, 26 Oct 2026 09:05:35 -0400 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E8=BF=87512kib=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/rental/RentalApiController.java | 4 +++- .../rental/RentalCallbackDemoController.java | 4 +++- .../system/rental/RentalBusinessTest.java | 20 +++++++++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalApiController.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalApiController.java index b7d94b2..5b3ed3e 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalApiController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalApiController.java @@ -46,7 +46,9 @@ public class RentalApiController { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int size; - while ((size = request.getInputStream().read(buffer)) != -1) { + // 可重复读取包装器每次getInputStream()都会从头创建新流,必须持有同一个流读取至EOF。 + javax.servlet.ServletInputStream input = request.getInputStream(); + while ((size = input.read(buffer)) != -1) { require(output.size() + size <= RentalJson.MAX_HTTP_BODY_BYTES, "请求体超过512KiB"); output.write(buffer, 0, size); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalCallbackDemoController.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalCallbackDemoController.java index 281c1e6..420b00a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalCallbackDemoController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalCallbackDemoController.java @@ -61,7 +61,9 @@ public class RentalCallbackDemoController { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int size; - while ((size = request.getInputStream().read(buffer)) != -1) { + // 与商户API一致:只获取一次流,避免可重复读取包装器不断重放同一份通知。 + javax.servlet.ServletInputStream input = request.getInputStream(); + while ((size = input.read(buffer)) != -1) { require(output.size() + size <= RentalJson.MAX_HTTP_BODY_BYTES, "通知超过512KiB"); output.write(buffer, 0, size); } diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java index 6897562..367facb 100644 --- a/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java +++ b/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java @@ -489,19 +489,23 @@ public class RentalBusinessTest { Map headers = map("X-Rent-Key", key, "X-Rent-Timestamp", Long.toString(timestamp), "X-Rent-Nonce", nonce, "X-Rent-Signature", RentalCrypto.sign(secret, scope, key, timestamp, nonce, "POST", path, body)); - java.io.ByteArrayInputStream input = new java.io.ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)); - javax.servlet.ServletInputStream stream = new javax.servlet.ServletInputStream() { - @Override public int read() { return input.read(); } - @Override public boolean isFinished() { return input.available() == 0; } - @Override public boolean isReady() { return true; } - @Override public void setReadListener(javax.servlet.ReadListener listener) { } - }; return (javax.servlet.http.HttpServletRequest) java.lang.reflect.Proxy.newProxyInstance( getClass().getClassLoader(), new Class[] {javax.servlet.http.HttpServletRequest.class}, (proxy, method, args) -> { if ("getHeader".equals(method.getName())) { return headers.get(args[0]); } if ("getRequestURI".equals(method.getName())) { return path; } - if ("getInputStream".equals(method.getName())) { return stream; } + if ("getInputStream".equals(method.getName())) { + // 模拟生产RepeatedlyRequestWrapper:每次调用返回从头开始的新流。 + // 若控制器在读取循环里反复获取流,小请求也会不断累加并触发大小限制。 + java.io.ByteArrayInputStream input = new java.io.ByteArrayInputStream( + body.getBytes(StandardCharsets.UTF_8)); + return new javax.servlet.ServletInputStream() { + @Override public int read() { return input.read(); } + @Override public boolean isFinished() { return input.available() == 0; } + @Override public boolean isReady() { return true; } + @Override public void setReadListener(javax.servlet.ReadListener listener) { } + }; + } return null; }); }