From 4b9cb3716b7a0c22c4cf1a7df35be7bf25f3b921 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Fri, 9 Feb 2018 09:20:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=B8=B4=E6=97=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=94=9F=E6=88=90=E7=9A=84=E7=9B=AE=E5=BD=95=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/net/http/MultiContext.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/org/redkale/net/http/MultiContext.java b/src/org/redkale/net/http/MultiContext.java index 343f5a6da..cd5a5a572 100644 --- a/src/org/redkale/net/http/MultiContext.java +++ b/src/org/redkale/net/http/MultiContext.java @@ -112,6 +112,19 @@ public final class MultiContext { return tmpfile; } + /** + * 根据临时文件获取上传时的文件名 + * + * @param file 临时文件 + * + * @return 上传的文件名 + */ + public static String getFileName(File file) { + if (file == null) return null; + String name = file.getName(); + return name.startsWith("redkale-") ? name.substring(name.indexOf('_') + 1) : name; + } + //或被 REST 用到 /** * 获取第一个文件 @@ -133,9 +146,9 @@ public final class MultiContext { has = true; if (filenameReg != null && !filenameReg.isEmpty() && !part.getFilename().matches(filenameReg)) continue; if (contentTypeReg != null && !contentTypeReg.isEmpty() && !part.getContentType().matches(contentTypeReg)) 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(); - parent.mkdirs(); + if (!parent.isDirectory()) parent.mkdirs(); boolean rs = part.save(max < 1 ? Long.MAX_VALUE : max, file); if (!rs) { file.delete(); @@ -165,9 +178,9 @@ public final class MultiContext { for (MultiPart part : parts()) { if (filenameReg != null && !filenameReg.isEmpty() && !part.getFilename().matches(filenameReg)) continue; if (contentTypeReg != null && !contentTypeReg.isEmpty() && !part.getContentType().matches(contentTypeReg)) 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(); - parent.mkdirs(); + if (!parent.isDirectory()) parent.mkdirs(); boolean rs = part.save(max < 1 ? Long.MAX_VALUE : max, file); if (!rs) { file.delete();