public static final HashMap map = new HashMap();
static {
map.put("89504E47", "png");
map.put("49492A00", "tif");
map.put("FFD8FF", "jpg");
}
public static String getFileType(String filePath) {
return (String) map.get(getFileHeader(filePath));
}
public static String getFileHeader(String filePath) {
FileInputStream is = null;
String value = null;
try {
is = new FileInputStream(filePath);
byte[] b = new byte[4];
/*
* int read() 从此输入流中读取一个数据字节。 int read(byte[] b) 从此输入流中将最多 b.length
* 个字节的数据读入一个 byte 数组中。 int read(byte[] b, int off, int len)
* 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
*/
is.read(b, 0, b.length);
value = bytesToHexString(b);
} catch (Exception e) {