mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
FileUtil.getMimeType增加webp识别
This commit is contained in:
parent
d91fc69da1
commit
609dc4d865
@ -10,6 +10,7 @@
|
|||||||
* 【db 】 优化count查询兼容informix(issue#I713XQ@Gitee)
|
* 【db 】 优化count查询兼容informix(issue#I713XQ@Gitee)
|
||||||
* 【core 】 去除Opt头部的GPL协议头(pr#995@Gitee)
|
* 【core 】 去除Opt头部的GPL协议头(pr#995@Gitee)
|
||||||
* 【core 】 邮箱校验添加对中文的支持(pr#997@Gitee)
|
* 【core 】 邮箱校验添加对中文的支持(pr#997@Gitee)
|
||||||
|
* 【core 】 FileUtil.getMimeType增加webp识别(pr#997@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复URLUtil.decode无法解码UTF-16问题(issue#3063@Github)
|
* 【core 】 修复URLUtil.decode无法解码UTF-16问题(issue#3063@Github)
|
||||||
|
@ -3483,23 +3483,27 @@ public class FileUtil extends PathUtil {
|
|||||||
* @since 4.1.15
|
* @since 4.1.15
|
||||||
*/
|
*/
|
||||||
public static String getMimeType(String filePath) {
|
public static String getMimeType(String filePath) {
|
||||||
String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath);
|
if(StrUtil.isBlank(filePath)){
|
||||||
if (null == contentType) {
|
return null;
|
||||||
// 补充一些常用的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";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 补充
|
// 补充一些常用的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) {
|
if (null == contentType) {
|
||||||
contentType = getMimeType(Paths.get(filePath));
|
contentType = getMimeType(Paths.get(filePath));
|
||||||
}
|
}
|
||||||
|
@ -446,7 +446,7 @@ public class FileUtilTest {
|
|||||||
|
|
||||||
mimeType = FileUtil.getMimeType("test.js");
|
mimeType = FileUtil.getMimeType("test.js");
|
||||||
// 在 jdk 11+ 会获取到 text/javascript,而非 自定义的 application/x-javascript
|
// 在 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));
|
Assert.assertTrue(list.contains(mimeType));
|
||||||
|
|
||||||
// office03
|
// office03
|
||||||
@ -468,6 +468,10 @@ public class FileUtilTest {
|
|||||||
// pr#2617@Github
|
// pr#2617@Github
|
||||||
mimeType = FileUtil.getMimeType("test.wgt");
|
mimeType = FileUtil.getMimeType("test.wgt");
|
||||||
Assert.assertEquals("application/widget", mimeType);
|
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
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user