-
推荐github项目: 根据扩展名得到content-type
https://github.com/j256/simplemagic -
使用
- 引入依赖
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.17</version>
</dependency>
- 主要代码
ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(扩展名);
String contentType = extensionMatch .getMimeType();
- 工具类
private String getMimeTypeByExtension(String extension){
String contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
if(StringUtils.isNotEmpty(extension)){
ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
if(extensionMatch!=null){
contentType = extensionMatch.getMimeType();
}
}
return contentType;
}