From 7e0a58d3d288ac0322b099ec238f0299b2cd8335 Mon Sep 17 00:00:00 2001 From: noear Date: Tue, 30 Sep 2025 12:10:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20MultipartConfig.fileSizeTh?= =?UTF-8?q?reshold=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feat/core/common/multipart/Part.java | 4 - .../feat/core/common/multipart/PartImpl.java | 74 ++++++++++++------- .../server/impl/MultipartFormDecoder.java | 6 +- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/feat-core/src/main/java/tech/smartboot/feat/core/common/multipart/Part.java b/feat-core/src/main/java/tech/smartboot/feat/core/common/multipart/Part.java index 055a0050..82c880d5 100644 --- a/feat-core/src/main/java/tech/smartboot/feat/core/common/multipart/Part.java +++ b/feat-core/src/main/java/tech/smartboot/feat/core/common/multipart/Part.java @@ -36,10 +36,6 @@ public interface Part { long getSize(); - - void write(String fileName) throws IOException; - - void delete() throws IOException; default String getHeader(HeaderName name) { diff --git a/feat-core/src/main/java/tech/smartboot/feat/core/common/multipart/PartImpl.java b/feat-core/src/main/java/tech/smartboot/feat/core/common/multipart/PartImpl.java index e6ffea80..cf3aef77 100644 --- a/feat-core/src/main/java/tech/smartboot/feat/core/common/multipart/PartImpl.java +++ b/feat-core/src/main/java/tech/smartboot/feat/core/common/multipart/PartImpl.java @@ -14,14 +14,8 @@ import tech.smartboot.feat.core.common.HeaderName; import tech.smartboot.feat.core.common.HeaderValue; import tech.smartboot.feat.core.common.FeatUtils; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -47,9 +41,9 @@ public class PartImpl implements Part { private OutputStream diskOutputStream; /** - * 表单域大小 + * 大小 */ - private int formSize; + private int partSize; /** * 磁盘文件 @@ -70,6 +64,9 @@ public class PartImpl implements Part { if (fileName == null && inputStream == null) { throw new IllegalStateException(); } + if (inputStream == null && thresholdBuffer != null) { + inputStream = new ByteArrayInputStream(thresholdBuffer.toByteArray()); + } if (inputStream != null) { return inputStream; } @@ -100,25 +97,10 @@ public class PartImpl implements Part { if (diskFile != null) { return diskFile.length(); } else { - return formSize; + return partSize; } } - @Override - public void write(String fileName) throws IOException { - if (this.diskFile == null) { - return; - } - Path target = Paths.get(fileName); - if (!target.isAbsolute()) { - if (FeatUtils.isNotBlank(multipartConfig.getLocation())) { - target = Paths.get(multipartConfig.getLocation(), fileName); - } else { - target = File.createTempFile("feat_" + this.hashCode(), fileName).toPath(); - } - } - Files.move(diskFile.toPath(), target, StandardCopyOption.REPLACE_EXISTING); - } @Override public void delete() throws IOException { @@ -206,10 +188,48 @@ public class PartImpl implements Part { } public void setFormSize(int formSize) { - this.formSize = formSize; + this.partSize = formSize; + } + + private ByteArrayOutputStream thresholdBuffer; + public void write(byte[] bytes) throws IOException { + if (multipartConfig.getFileSizeThreshold() > 0) { + //如果有 FileSizeThreshold 配置? + if (thresholdBuffer == null) { + thresholdBuffer = new ByteArrayOutputStream(); + } + + if (thresholdBuffer.size() < multipartConfig.getFileSizeThreshold()) { + thresholdBuffer.write(bytes); + } else { + //如果超出了 FileSizeThreshold 大小? + getDiskOutputStream().write(thresholdBuffer.toByteArray()); + getDiskOutputStream().write(bytes); + } + } else { + getDiskOutputStream().write(bytes); + } + } + + public void flush() throws IOException { + if(diskOutputStream != null) { + diskOutputStream.flush(); + thresholdBuffer = null; + } + + if(thresholdBuffer != null){ + partSize = thresholdBuffer.size(); + } + } + + public void close() throws IOException { + if (diskOutputStream != null) { + diskOutputStream.close(); + thresholdBuffer = null; + } } - public OutputStream getDiskOutputStream() throws IOException { + private OutputStream getDiskOutputStream() throws IOException { if (fileName == null) { throw new IllegalStateException(); } diff --git a/feat-core/src/main/java/tech/smartboot/feat/core/server/impl/MultipartFormDecoder.java b/feat-core/src/main/java/tech/smartboot/feat/core/server/impl/MultipartFormDecoder.java index ba3213fe..99b0ba60 100644 --- a/feat-core/src/main/java/tech/smartboot/feat/core/server/impl/MultipartFormDecoder.java +++ b/feat-core/src/main/java/tech/smartboot/feat/core/server/impl/MultipartFormDecoder.java @@ -218,13 +218,13 @@ class MultipartFormDecoder { } } try { - currentPart.getDiskOutputStream().write(bytes); + currentPart.write(bytes); if (boundaryLimit >= 0) { if (byteBuffer.get() != FeatUtils.CR || byteBuffer.get() != FeatUtils.LF) { throw new HttpException(HttpStatus.BAD_REQUEST); } - currentPart.getDiskOutputStream().flush(); - currentPart.getDiskOutputStream().close(); + currentPart.flush(); + currentPart.close(); currentPart = null; state = STATE_END_CHECK; -- Gitee