FileUtil.getMimeType增加webp识别

This commit is contained in:
Looly 2023-05-14 23:49:28 +08:00
parent d91fc69da1
commit 609dc4d865
3 changed files with 25 additions and 16 deletions

View File

@ -10,6 +10,7 @@
* 【db 】 优化count查询兼容informixissue#I713XQ@Gitee
* 【core 】 去除Opt头部的GPL协议头pr#995@Gitee
* 【core 】 邮箱校验添加对中文的支持pr#997@Gitee
* 【core 】 FileUtil.getMimeType增加webp识别pr#997@Gitee
### 🐞Bug修复
* 【core 】 修复URLUtil.decode无法解码UTF-16问题issue#3063@Github

View File

@ -3483,23 +3483,27 @@ public class FileUtil extends PathUtil {
* @since 4.1.15
*/
public static String getMimeType(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";
} else if (StrUtil.endWithIgnoreCase(filePath, ".webp")) {
// JDK8不支持
return "image/webp";
}
String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath);
if (null == contentType) {
contentType = getMimeType(Paths.get(filePath));
}

View File

@ -446,7 +446,7 @@ public class FileUtilTest {
mimeType = FileUtil.getMimeType("test.js");
// jdk 11+ 会获取到 text/javascript,而非 自定义的 application/x-javascript
List<String> list = ListUtil.of("text/javascript", "application/x-javascript");
final List<String> list = ListUtil.of("text/javascript", "application/x-javascript");
Assert.assertTrue(list.contains(mimeType));
// office03
@ -468,6 +468,10 @@ public class FileUtilTest {
// pr#2617@Github
mimeType = FileUtil.getMimeType("test.wgt");
Assert.assertEquals("application/widget", mimeType);
// issue#3092
mimeType = FileUtil.getMimeType("https://xxx.oss-cn-hangzhou.aliyuncs.com/xxx.webp");
Assert.assertEquals("image/webp", mimeType);
}
@Test