From 039130402487a5abdb8a0c49b8b6701883e3b4cc Mon Sep 17 00:00:00 2001 From: redkale Date: Fri, 10 Nov 2023 22:42:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/net/http/MimeType.java | 9 +++++++-- .../org/redkale/net/http/MultiContext.java | 10 +++++----- .../java/org/redkale/net/http/MultiPart.java | 19 +++++++++++++++---- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/redkale/net/http/MimeType.java b/src/main/java/org/redkale/net/http/MimeType.java index 04140dd42..07cef5526 100644 --- a/src/main/java/org/redkale/net/http/MimeType.java +++ b/src/main/java/org/redkale/net/http/MimeType.java @@ -17,7 +17,7 @@ import java.util.*; */ public class MimeType { - private final static Map contentTypes = new HashMap<>(); + private static final Map contentTypes = new HashMap<>(); static { contentTypes.put("abs", "audio/x-mpeg"); @@ -177,6 +177,9 @@ public class MimeType { contentTypes.put("zip", "application/zip"); } + private MimeType() { + } + public static String get(String extension) { return contentTypes.getOrDefault(extension.toLowerCase(), "text/plain"); } @@ -198,7 +201,9 @@ public class MimeType { public static String getByFilename(String fileName) { int length = fileName.length(); int newEnd = fileName.lastIndexOf('#'); - if (newEnd == -1) newEnd = length; + if (newEnd == -1) { + newEnd = length; + } int i = fileName.lastIndexOf('.', newEnd); return (i < 0) ? null : get(fileName.substring(i + 1, newEnd).toLowerCase()); } diff --git a/src/main/java/org/redkale/net/http/MultiContext.java b/src/main/java/org/redkale/net/http/MultiContext.java index 4e608673e..a97d39b73 100644 --- a/src/main/java/org/redkale/net/http/MultiContext.java +++ b/src/main/java/org/redkale/net/http/MultiContext.java @@ -108,7 +108,7 @@ public final class MultiContext { continue;//不遍历完后面getParameter可能获取不到值 } has = true; - if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFilename().matches(fileNameRegx)) { + if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFileName().matches(fileNameRegx)) { continue; } if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) { @@ -157,13 +157,13 @@ public final class MultiContext { continue; //不遍历完后面getParameter可能获取不到值 } has = true; - if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFilename().matches(fileNameRegx)) { + if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFileName().matches(fileNameRegx)) { continue; } if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) { continue; } - File file = new File(home, "tmp/redkale-" + System.nanoTime() + "_" + part.getFilename()); + File file = new File(home, "tmp/redkale-" + System.nanoTime() + "_" + part.getFileName()); File parent = file.getParentFile(); if (!parent.isDirectory()) { parent.mkdirs(); @@ -197,13 +197,13 @@ public final class MultiContext { } List files = null; for (MultiPart part : parts()) { - if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFilename().matches(fileNameRegx)) { + if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFileName().matches(fileNameRegx)) { continue; } if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) { continue; } - File file = new File(home, "tmp/redkale-" + System.nanoTime() + "_" + part.getFilename()); + File file = new File(home, "tmp/redkale-" + System.nanoTime() + "_" + part.getFileName()); File parent = file.getParentFile(); if (!parent.isDirectory()) { parent.mkdirs(); diff --git a/src/main/java/org/redkale/net/http/MultiPart.java b/src/main/java/org/redkale/net/http/MultiPart.java index 61d7bf977..2c5a9a614 100644 --- a/src/main/java/org/redkale/net/http/MultiPart.java +++ b/src/main/java/org/redkale/net/http/MultiPart.java @@ -17,7 +17,7 @@ import java.util.concurrent.atomic.LongAdder; */ public final class MultiPart { - private final String filename; + private final String fileName; private final String name; @@ -28,7 +28,7 @@ public final class MultiPart { private final LongAdder received; MultiPart(String fileName, String name, String contentType, LongAdder received, InputStream in) { - this.filename = fileName; + this.fileName = fileName; this.name = name; this.in = in; this.contentType = contentType; @@ -37,7 +37,7 @@ public final class MultiPart { @Override public String toString() { - return this.getClass().getSimpleName() + "{" + "name=" + name + ", filename=" + filename + ", contentType=" + contentType + ", received=" + received + '}'; + return this.getClass().getSimpleName() + "{" + "name=" + name + ", fileName=" + fileName + ", contentType=" + contentType + ", received=" + received + '}'; } public boolean save(File file) throws IOException { @@ -99,8 +99,19 @@ public final class MultiPart { return contentType; } + public String getFileName() { + return fileName; + } + + /** + * @see #getFileName() + * + * @return String + * @deprecated replace by {@link #getFileName() } + */ + @Deprecated(since = "2.8.0") public String getFilename() { - return filename; + return fileName; } public String getName() {