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 055a0050d3beb3581f30d6d8013128ad92939d1d..82c880d5ce6978b14f1dc8220acab6cc700085cf 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 e6ffea806d97e25bfabdf55f416e7ecab0d421d3..cf3aef77800156853161d02287769171669a6895 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 ba3213fee4ef9ece9968456f586185f81d067014..99b0ba602c79cc325e7822f4056d6cb119e6e5cb 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;