diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java index a8657dfc9..8b77b5714 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java @@ -2680,23 +2680,24 @@ public class FileUtil extends PathUtil { * @since 4.1.15 */ public static String getMimeType(final String filePath) { - String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath); - if (null == contentType) { - // 补充一些常用的mimeType - if (StrUtil.endWithIgnoreCase(filePath, ".css")) { - contentType = "text/css"; - } else if (StrUtil.endWithIgnoreCase(filePath, ".js")) { - contentType = "application/x-javascript"; - } else if (StrUtil.endWithIgnoreCase(filePath, ".rar")) { - contentType = "application/x-rar-compressed"; - } else if (StrUtil.endWithIgnoreCase(filePath, ".7z")) { - contentType = "application/x-7z-compressed"; - } else if (StrUtil.endWithIgnoreCase(filePath, ".wgt")) { - contentType = "application/widget"; - } + if(StrUtil.isBlank(filePath)){ + return null; } - // 补充 + // 补充一些常用的mimeType + if (StrUtil.endWithIgnoreCase(filePath, ".css")) { + return "text/css"; + } else if (StrUtil.endWithIgnoreCase(filePath, ".js")) { + return "application/x-javascript"; + } else if (StrUtil.endWithIgnoreCase(filePath, ".rar")) { + return "application/x-rar-compressed"; + } else if (StrUtil.endWithIgnoreCase(filePath, ".7z")) { + return "application/x-7z-compressed"; + } else if (StrUtil.endWithIgnoreCase(filePath, ".wgt")) { + return "application/widget"; + } + + String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath); if (null == contentType) { contentType = getMimeType(Paths.get(filePath)); } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/io/FileUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/io/FileUtilTest.java index 9dd631324..759b709dd 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/io/FileUtilTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/io/FileUtilTest.java @@ -431,6 +431,10 @@ public class FileUtilTest { // pr#2617@Github mimeType = FileUtil.getMimeType("test.wgt"); Assertions.assertEquals("application/widget", mimeType); + + // issue#3092 + mimeType = FileUtil.getMimeType("https://xxx.oss-cn-hangzhou.aliyuncs.com/xxx.webp"); + Assertions.assertEquals("image/webp", mimeType); } @Test