FileTypeUtil增加null判断

This commit is contained in:
Looly 2023-12-11 09:45:06 +08:00
parent 1e06dd4674
commit 4ab74605be
2 changed files with 43 additions and 36 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.24(2023-12-09)
# 5.8.24(2023-12-11)
### 🐣新特性
* 【cache 】 Cache增加get重载可自定义超时时间issue#I8G0DL@Gitee
@ -10,6 +10,7 @@
* 【db 】 增加识别OpenGauss的驱动类issue#I8K6C0@Gitee
* 【core 】 修复CharSequenceUtil注释和引用避免循环引用
* 【extra 】 SpringUtil增加getProperty重载pr#1122@Gitee
* 【core 】 FileTypeUtil增加null判断issue#3419@Github
### 🐞Bug修复
* 【core 】 修复LocalDateTime#parseDate未判断空问题问题issue#I8FN7F@Gitee

View File

@ -53,7 +53,7 @@ public class FileTypeUtil {
* @return 文件类型未找到为{@code null}
*/
public static String getType(String fileStreamHexHead) {
if(MapUtil.isNotEmpty(FILE_TYPE_MAP)){
if (MapUtil.isNotEmpty(FILE_TYPE_MAP)) {
for (final Entry<String, String> fileTypeEntry : FILE_TYPE_MAP.entrySet()) {
if (StrUtil.startWithIgnoreCase(fileStreamHexHead, fileTypeEntry.getKey())) {
return fileTypeEntry.getValue();
@ -67,39 +67,44 @@ public class FileTypeUtil {
/**
* 根据文件流的头部信息获得文件类型
*
* @param in 文件流
* @param in 文件流
* @param fileHeadSize 自定义读取文件头部的大小
* @return 文件类型未找到为{@code null}
*/
public static String getType(InputStream in,int fileHeadSize) throws IORuntimeException {
return getType((IoUtil.readHex(in, fileHeadSize,false)));
public static String getType(InputStream in, int fileHeadSize) throws IORuntimeException {
return getType((IoUtil.readHex(in, fileHeadSize, false)));
}
/**
* 根据文件流的头部信息获得文件类型<br>
* 注意此方法会读取头部一些bytes造成此流接下来读取时缺少部分bytes<br>
* 因此如果想复用此流流需支持{@link InputStream#reset()}方法
* @param in {@link InputStream}
*
* @param in {@link InputStream}
* @param isExact 是否精确匹配如果为false使用前64个bytes匹配如果为true使用前8192bytes匹配
* @return 类型文件的扩展名未找到为{@code null}
* @throws IORuntimeException 读取流引起的异常
* @return 类型文件的扩展名提供的in为{@code null}未找到为{@code null}
* @throws IORuntimeException 读取流引起的异常
*/
public static String getType(InputStream in,boolean isExact) throws IORuntimeException {
public static String getType(InputStream in, boolean isExact) throws IORuntimeException {
if (null == in) {
return null;
}
return isExact
?getType(IoUtil.readHex8192Upper(in))
:getType(IoUtil.readHex64Upper(in));
? getType(IoUtil.readHex8192Upper(in))
: getType(IoUtil.readHex64Upper(in));
}
/**
* 根据文件流的头部信息获得文件类型<br>
* 注意此方法会读取头部64个bytes造成此流接下来读取时缺少部分bytes<br>
* 因此如果想复用此流流需支持{@link InputStream#reset()}方法
*
* @param in {@link InputStream}
* @return 类型文件的扩展名未找到为{@code null}
* @throws IORuntimeException 读取流引起的异常
* @throws IORuntimeException 读取流引起的异常
*/
public static String getType(InputStream in) throws IORuntimeException {
return getType(in,false);
public static String getType(InputStream in) throws IORuntimeException {
return getType(in, false);
}
/**
@ -116,10 +121,10 @@ public class FileTypeUtil {
* @param in {@link InputStream}
* @param filename 文件名
* @return 类型文件的扩展名未找到为{@code null}
* @throws IORuntimeException 读取流引起的异常
* @throws IORuntimeException 读取流引起的异常
*/
public static String getType(InputStream in, String filename) throws IORuntimeException {
return getType(in,filename,false);
public static String getType(InputStream in, String filename) throws IORuntimeException {
return getType(in, filename, false);
}
/**
@ -132,14 +137,15 @@ public class FileTypeUtil {
* 2xlsdocmsi头信息无法区分按照扩展名区分
* 3zip可能为docxxlsxpptxjarwarofd头信息无法区分按照扩展名区分
* </pre>
*
* @param in {@link InputStream}
* @param filename 文件名
* @param isExact 是否精确匹配如果为false使用前64个bytes匹配如果为true使用前8192bytes匹配
* @param isExact 是否精确匹配如果为false使用前64个bytes匹配如果为true使用前8192bytes匹配
* @return 类型文件的扩展名未找到为{@code null}
* @throws IORuntimeException 读取流引起的异常
* @throws IORuntimeException 读取流引起的异常
*/
public static String getType(InputStream in, String filename,boolean isExact) throws IORuntimeException {
String typeName = getType(in,isExact);
public static String getType(InputStream in, String filename, boolean isExact) throws IORuntimeException {
String typeName = getType(in, isExact);
if (null == typeName) {
// 未成功识别类型扩展名辅助识别
typeName = FileUtil.extName(filename);
@ -190,19 +196,19 @@ public class FileTypeUtil {
* 3zip可能为jarwar头信息无法区分按照扩展名区分
* </pre>
*
* @param file 文件 {@link File}
* @param file 文件 {@link File}
* @param isExact 是否精确匹配如果为false使用前64个bytes匹配如果为true使用前8192bytes匹配
* @return 类型文件的扩展名未找到为{@code null}
* @throws IORuntimeException 读取文件引起的异常
* @throws IORuntimeException 读取文件引起的异常
*/
public static String getType(File file,boolean isExact) throws IORuntimeException {
if(false == FileUtil.isFile(file)){
public static String getType(File file, boolean isExact) throws IORuntimeException {
if (false == FileUtil.isFile(file)) {
throw new IllegalArgumentException("Not a regular file!");
}
FileInputStream in = null;
try {
in = IoUtil.toStream(file);
return getType(in, file.getName(),isExact);
return getType(in, file.getName(), isExact);
} finally {
IoUtil.close(in);
}
@ -219,22 +225,22 @@ public class FileTypeUtil {
*
* @param file 文件 {@link File}
* @return 类型文件的扩展名未找到为{@code null}
* @throws IORuntimeException 读取文件引起的异常
* @throws IORuntimeException 读取文件引起的异常
*/
public static String getType(File file) throws IORuntimeException {
return getType(file,false);
public static String getType(File file) throws IORuntimeException {
return getType(file, false);
}
/**
* 通过路径获得文件类型
*
* @param path 路径绝对路径或相对ClassPath的路径
* @param path 路径绝对路径或相对ClassPath的路径
* @param isExact 是否精确匹配如果为false使用前64个bytes匹配如果为true使用前8192bytes匹配
* @return 类型
* @throws IORuntimeException 读取文件引起的异常
* @throws IORuntimeException 读取文件引起的异常
*/
public static String getTypeByPath(String path,boolean isExact) throws IORuntimeException {
return getType(FileUtil.file(path),isExact);
public static String getTypeByPath(String path, boolean isExact) throws IORuntimeException {
return getType(FileUtil.file(path), isExact);
}
/**
@ -242,10 +248,10 @@ public class FileTypeUtil {
*
* @param path 路径绝对路径或相对ClassPath的路径
* @return 类型
* @throws IORuntimeException 读取文件引起的异常
* @throws IORuntimeException 读取文件引起的异常
*/
public static String getTypeByPath(String path) throws IORuntimeException {
return getTypeByPath(path,false);
public static String getTypeByPath(String path) throws IORuntimeException {
return getTypeByPath(path, false);
}