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