diff --git a/__hatch/solon.boot.reactor-netty/src/main/java/org/noear/solon/boot/reactornetty/HttpRequestParser.java b/__hatch/solon.boot.reactor-netty/src/main/java/org/noear/solon/boot/reactornetty/HttpRequestParser.java index f7ec6dc460..eb5532a883 100644 --- a/__hatch/solon.boot.reactor-netty/src/main/java/org/noear/solon/boot/reactornetty/HttpRequestParser.java +++ b/__hatch/solon.boot.reactor-netty/src/main/java/org/noear/solon/boot/reactornetty/HttpRequestParser.java @@ -89,7 +89,7 @@ protected HttpRequestParser parse() throws Exception { extension = name.substring(idx + 1); } - UploadedFile f1 = new UploadedFile(null, contentType, contentSize, content, name, extension); + UploadedFile f1 = new UploadedFile(f0::delete, contentType, contentSize, content, name, extension); tmp.add(f1); } diff --git a/solon-projects/solon-boot/solon.boot.jdkhttp/src/main/java/org/noear/solon/boot/jdkhttp/MultipartUtil.java b/solon-projects/solon-boot/solon.boot.jdkhttp/src/main/java/org/noear/solon/boot/jdkhttp/MultipartUtil.java index 73cfff5bf8..99bc3c1297 100644 --- a/solon-projects/solon-boot/solon.boot.jdkhttp/src/main/java/org/noear/solon/boot/jdkhttp/MultipartUtil.java +++ b/solon-projects/solon-boot/solon.boot.jdkhttp/src/main/java/org/noear/solon/boot/jdkhttp/MultipartUtil.java @@ -3,27 +3,25 @@ import com.sun.net.httpserver.HttpExchange; import org.noear.solon.boot.ServerProps; +import org.noear.solon.boot.http.HttpPartFile; import org.noear.solon.boot.jdkhttp.uploadfile.HttpMultipart; import org.noear.solon.boot.jdkhttp.uploadfile.HttpMultipartCollection; import org.noear.solon.boot.io.LimitedInputStream; import org.noear.solon.core.handle.UploadedFile; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; class MultipartUtil { - public static void buildParamsAndFiles(JdkHttpContext context) throws IOException{ + public static void buildParamsAndFiles(JdkHttpContext context) throws IOException { HttpMultipartCollection parts = new HttpMultipartCollection((HttpExchange) context.request()); - while (parts.hasNext()){ + while (parts.hasNext()) { HttpMultipart part = parts.next(); - if(isFile(part) == false){ + if (isFile(part) == false) { context.paramSet(part.name, part.getString()); - }else{ + } else { doBuildFiles(context, part); } } @@ -37,8 +35,7 @@ private static void doBuildFiles(JdkHttpContext context, HttpMultipart part) thr } String contentType = part.getHeaders().get("Content-Type"); - InputStream content = read(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize)); - int contentSize = content.available(); + HttpPartFile partFile = new HttpPartFile(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize)); String name = part.getFilename(); String extension = null; int idx = name.lastIndexOf("."); @@ -46,27 +43,16 @@ private static void doBuildFiles(JdkHttpContext context, HttpMultipart part) thr extension = name.substring(idx + 1); } - UploadedFile f1 = new UploadedFile(null, contentType, contentSize, content, name, extension); + UploadedFile f1 = new UploadedFile(partFile::delete, contentType, partFile.getSize(), partFile.getContent(), name, extension); list.add(f1); } - private static boolean isField(HttpMultipart filePart){ + private static boolean isField(HttpMultipart filePart) { return filePart.getFilename() == null; } - private static boolean isFile(HttpMultipart filePart){ + private static boolean isFile(HttpMultipart filePart) { return !isField(filePart); } - - private static ByteArrayInputStream read(InputStream input) throws IOException { - ByteArrayOutputStream output = new ByteArrayOutputStream(); - - byte[] buffer = new byte[4096]; - int n = 0; - while (-1 != (n = input.read(buffer))) { - output.write(buffer, 0, n); - } - return new ByteArrayInputStream(output.toByteArray()); - } } diff --git a/solon-projects/solon-boot/solon.boot.jlhttp/src/main/java/org/noear/solon/boot/jlhttp/MultipartUtil.java b/solon-projects/solon-boot/solon.boot.jlhttp/src/main/java/org/noear/solon/boot/jlhttp/MultipartUtil.java index 132d0887a9..9d1546378f 100644 --- a/solon-projects/solon-boot/solon.boot.jlhttp/src/main/java/org/noear/solon/boot/jlhttp/MultipartUtil.java +++ b/solon-projects/solon-boot/solon.boot.jlhttp/src/main/java/org/noear/solon/boot/jlhttp/MultipartUtil.java @@ -2,6 +2,7 @@ import org.noear.jlhttp.HTTPServer; import org.noear.solon.boot.ServerProps; +import org.noear.solon.boot.http.HttpPartFile; import org.noear.solon.boot.io.LimitedInputStream; import org.noear.solon.core.handle.UploadedFile; @@ -37,8 +38,7 @@ private static void doBuildFiles(JlHttpContext context, HTTPServer.MultipartIter String contentType = part.getHeaders().get("Content-Type"); - InputStream content = read(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize)); - int contentSize = content.available(); + HttpPartFile partFile = new HttpPartFile(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize)); String name = part.getFilename(); String extension = null; int idx = name.lastIndexOf("."); @@ -46,7 +46,7 @@ private static void doBuildFiles(JlHttpContext context, HTTPServer.MultipartIter extension = name.substring(idx + 1); } - UploadedFile f1 = new UploadedFile(null, contentType, contentSize, content, name, extension); + UploadedFile f1 = new UploadedFile(partFile::delete, contentType, partFile.getSize(), partFile.getContent(), name, extension); list.add(f1); } @@ -59,14 +59,4 @@ private static boolean isFile(HTTPServer.MultipartIterator.Part filePart) { return !isField(filePart); } - private static ByteArrayInputStream read(InputStream input) throws IOException { - ByteArrayOutputStream output = new ByteArrayOutputStream(); - - byte[] buffer = new byte[4096]; - int n = 0; - while (-1 != (n = input.read(buffer))) { - output.write(buffer, 0, n); - } - return new ByteArrayInputStream(output.toByteArray()); - } } diff --git a/solon-projects/solon-boot/solon.boot.smarthttp/src/main/java/org/noear/solon/boot/smarthttp/http/MultipartUtil.java b/solon-projects/solon-boot/solon.boot.smarthttp/src/main/java/org/noear/solon/boot/smarthttp/http/MultipartUtil.java index 88916e6d60..64068d80f8 100644 --- a/solon-projects/solon-boot/solon.boot.smarthttp/src/main/java/org/noear/solon/boot/smarthttp/http/MultipartUtil.java +++ b/solon-projects/solon-boot/solon.boot.smarthttp/src/main/java/org/noear/solon/boot/smarthttp/http/MultipartUtil.java @@ -2,6 +2,7 @@ import org.noear.solon.boot.ServerProps; +import org.noear.solon.boot.http.HttpPartFile; import org.noear.solon.boot.smarthttp.http.uploadfile.HttpMultipart; import org.noear.solon.boot.smarthttp.http.uploadfile.HttpMultipartCollection; import org.noear.solon.boot.io.LimitedInputStream; @@ -39,8 +40,7 @@ private static void doBuildFiles(SmHttpContext context, HttpMultipart part) thro } String contentType = part.getHeaders().get("Content-Type"); - InputStream content = read(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize)); - int contentSize = content.available(); + HttpPartFile partFile = new HttpPartFile(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize)); String name = part.getFilename(); String extension = null; int idx = name.lastIndexOf("."); @@ -48,7 +48,7 @@ private static void doBuildFiles(SmHttpContext context, HttpMultipart part) thro extension = name.substring(idx + 1); } - UploadedFile f1 = new UploadedFile(null,contentType, contentSize, content, name, extension); + UploadedFile f1 = new UploadedFile(partFile::delete,contentType, partFile.getSize(), partFile.getContent(), name, extension); list.add(f1); } @@ -61,14 +61,4 @@ private static boolean isFile(HttpMultipart filePart){ return !isField(filePart); } - private static ByteArrayInputStream read(InputStream input) throws IOException { - ByteArrayOutputStream output = new ByteArrayOutputStream(); - - byte[] buffer = new byte[4096]; - int n = 0; - while (-1 != (n = input.read(buffer))) { - output.write(buffer, 0, n); - } - return new ByteArrayInputStream(output.toByteArray()); - } } diff --git a/solon-projects/solon-boot/solon.boot/src/main/java/org/noear/solon/boot/http/HttpPartFile.java b/solon-projects/solon-boot/solon.boot/src/main/java/org/noear/solon/boot/http/HttpPartFile.java new file mode 100644 index 0000000000..e1be2bb539 --- /dev/null +++ b/solon-projects/solon-boot/solon.boot/src/main/java/org/noear/solon/boot/http/HttpPartFile.java @@ -0,0 +1,58 @@ +package org.noear.solon.boot.http; + +import org.noear.solon.Utils; +import org.noear.solon.boot.ServerProps; +import org.noear.solon.core.util.IoUtil; + +import java.io.*; +import java.nio.file.Files; + +/** + * 临时文件 + * + * @author noear + * @since 2.7 + */ +public class HttpPartFile { + private File tmpfile; + private InputStream inputStream; + + public HttpPartFile(InputStream ins) throws IOException { + if (ServerProps.request_useTempfile) { + tmpfile = Files.createTempFile(Utils.guid(), ".tmp").toFile(); + try (OutputStream outs = new FileOutputStream(tmpfile)) { + IoUtil.transferTo(ins, outs); + } + + inputStream = new FileInputStream(tmpfile); + } else { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + IoUtil.transferTo(ins, output); + + inputStream = new ByteArrayInputStream(output.toByteArray()); + } + } + + /** + * 删除 + */ + public void delete() throws IOException { + if (tmpfile != null) { + tmpfile.delete(); + } + } + + /** + * 获取内容 + */ + public InputStream getContent() throws IOException { + return inputStream; + } + + /** + * 获取大小 + */ + public int getSize() throws IOException { + return inputStream.available(); + } +}