mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
新增读取流无法识别类型按照扩展名识别
This commit is contained in:
parent
5cdbd16c0b
commit
47134c0536
@ -133,6 +133,7 @@ public class FileTypeUtil {
|
||||
return getType(IoUtil.readHex28Upper(in));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据文件流的头部信息获得文件类型
|
||||
*
|
||||
@ -141,27 +142,20 @@ public class FileTypeUtil {
|
||||
* 2、xls、doc、msi头信息无法区分,按照扩展名区分
|
||||
* 3、zip可能为docx、xlsx、pptx、jar、war头信息无法区分,按照扩展名区分
|
||||
* </pre>
|
||||
*
|
||||
* @param file 文件 {@link File}
|
||||
* @param in {@link InputStream}
|
||||
* @param filename 文件名
|
||||
* @return 类型,文件的扩展名,未找到为<code>null</code>
|
||||
* @throws IORuntimeException 读取文件引起的异常
|
||||
* @throws IORuntimeException 读取流引起的异常
|
||||
*/
|
||||
public static String getType(File file) throws IORuntimeException {
|
||||
String typeName;
|
||||
FileInputStream in = null;
|
||||
try {
|
||||
in = IoUtil.toStream(file);
|
||||
typeName = getType(in);
|
||||
} finally {
|
||||
IoUtil.close(in);
|
||||
}
|
||||
public static String getType(InputStream in, String filename) {
|
||||
String typeName = getType(in);
|
||||
|
||||
if (null == typeName) {
|
||||
// 未成功识别类型,扩展名辅助识别
|
||||
typeName = FileUtil.extName(file);
|
||||
typeName = FileUtil.extName(filename);
|
||||
} else if ("xls".equals(typeName)) {
|
||||
// xls、doc、msi的头一样,使用扩展名辅助判断
|
||||
final String extName = FileUtil.extName(file);
|
||||
final String extName = FileUtil.extName(filename);
|
||||
if ("doc".equalsIgnoreCase(extName)) {
|
||||
typeName = "doc";
|
||||
} else if ("msi".equalsIgnoreCase(extName)) {
|
||||
@ -169,7 +163,7 @@ public class FileTypeUtil {
|
||||
}
|
||||
} else if ("zip".equals(typeName)) {
|
||||
// zip可能为docx、xlsx、pptx、jar、war等格式,扩展名辅助判断
|
||||
final String extName = FileUtil.extName(file);
|
||||
final String extName = FileUtil.extName(filename);
|
||||
if ("docx".equalsIgnoreCase(extName)) {
|
||||
typeName = "docx";
|
||||
} else if ("xlsx".equalsIgnoreCase(extName)) {
|
||||
@ -185,6 +179,29 @@ public class FileTypeUtil {
|
||||
return typeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件流的头部信息获得文件类型
|
||||
*
|
||||
* <pre>
|
||||
* 1、无法识别类型默认按照扩展名识别
|
||||
* 2、xls、doc、msi头信息无法区分,按照扩展名区分
|
||||
* 3、zip可能为docx、xlsx、pptx、jar、war头信息无法区分,按照扩展名区分
|
||||
* </pre>
|
||||
*
|
||||
* @param file 文件 {@link File}
|
||||
* @return 类型,文件的扩展名,未找到为<code>null</code>
|
||||
* @throws IORuntimeException 读取文件引起的异常
|
||||
*/
|
||||
public static String getType(File file) throws IORuntimeException {
|
||||
FileInputStream in = null;
|
||||
try {
|
||||
in = IoUtil.toStream(file);
|
||||
return getType(in, file.getName());
|
||||
} finally {
|
||||
IoUtil.close(in);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过路径获得文件类型
|
||||
*
|
||||
|
@ -6,6 +6,7 @@ import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -56,4 +57,13 @@ public class FileTypeUtilTest {
|
||||
Assert.assertEquals("ofd", type);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void inputStreamAndFilenameTest() {
|
||||
File file = FileUtil.file("e:/laboratory/test.xlsx");
|
||||
String type = FileTypeUtil.getType(file);
|
||||
Assert.assertEquals("xlsx", type);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user