From 24637169c36f6d34fc4131c322016ab6144dc5f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Sat, 8 Jan 2022 13:13:17 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=B8=8A=E4=BC=A0=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/rymcu/forest/util/FileUtils.java | 20 ++++++++++++------- .../web/api/common/UploadController.java | 3 +-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/rymcu/forest/util/FileUtils.java b/src/main/java/com/rymcu/forest/util/FileUtils.java index c0ed2b7..61ee8ab 100644 --- a/src/main/java/com/rymcu/forest/util/FileUtils.java +++ b/src/main/java/com/rymcu/forest/util/FileUtils.java @@ -7,7 +7,6 @@ import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.net.URLEncoder; -import java.util.Arrays; /** * 文件操作工具类 @@ -35,13 +34,20 @@ public class FileUtils { * @return */ public static String getExtend(String filename, String defExt) { - if ((filename != null) && (filename.length() > 0)) { - int i = filename.lastIndexOf('.'); - - if ((i > 0) && (i < (filename.length() - 1))) { - return (filename.substring(i + 1)).toLowerCase(); - } + if (StringUtils.isEmpty(filename)) { + return defExt.toLowerCase(); } + + int i = filename.lastIndexOf('.'); + + if ((i > 0) && (i < (filename.length() - 1))) { + String result = filename.substring(i + 1).toLowerCase(); + if (result.contains("?")) { + return result.split("\\?")[0]; + } + return result; + } + return defExt.toLowerCase(); } diff --git a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java index 44a2a14..c0e9455 100644 --- a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java @@ -187,8 +187,7 @@ public class UploadController { String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/"; - String orgName = url.substring(url.lastIndexOf(".")); - String fileName = System.currentTimeMillis() + FileUtils.getExtend(orgName).toLowerCase(); + String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url); String savePath = file.getPath() + File.separator + fileName;