根据文件的后缀名识别文件类型并不准确,可以使用文件的头信息进行识别:
以下是各类文件的头:
JPEG (jpg),文件头:FFD8FF
PNG (png),文件头:89504E47
GIF (gif),文件头:47494638
TIFF (tif),文件头:49492A00
Windows Bitmap (bmp),文件头:424D
CAD (dwg),文件头:41433130
Adobe Photoshop (psd),文件头:38425053
Rich Text Format (rtf),文件头:7B5C727466
XML (xml),文件头:3C3F786D6C
HTML (html),文件头:68746D6C3E
Email [thorough only] (eml),文件头:44656C69766572792D646174653A
Outlook Express (dbx),文件头:CFAD12FEC5FD746F
Outlook (pst),文件头:2142444E
MS Word/Excel (xls.or.doc),文件头:D0CF11E0
MS Access (mdb),文件头:5374616E64617264204A
WordPerfect (wpd),文件头:FF575043
Postscript (eps.or.ps),文件头:252150532D41646F6265
Adobe Acrobat (pdf),文件头:255044462D312E
Quicken (qdf),文件头:AC9EBD8F
Windows Password (pwl),文件头:E3828596
ZIP Archive (zip),文件头:504B0304
RAR Archive (rar),文件头:52617221
Wave (wav),文件头:57415645
AVI (avi),文件头:41564920
Real Audio (ram),文件头:2E7261FD
Real Media (rm),文件头:2E524D46
MPEG (mpg),文件头:000001BA
MPEG (mpg),文件头:000001B3
Quicktime (mov),文件头:6D6F6F76
Windows Media (asf),文件头:3026B2758E66CF11
MIDI (mid),文件头:4D546864
检测文件类型的代码如下:
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- public class FileTypeDetector {
- private static Map<String,String> head2FileType = new HashMap<String,String>();
- static{
- head2FileType.put("FFD8FFE1", "jpg");
- head2FileType.put("89504E47", "png");
- head2FileType.put("47494638 ", "gif");
- head2FileType.put("49492A00", "tif");
- head2FileType.put("424D", "bmp");
- head2FileType.put("41433130", "dwg");
- head2FileType.put("38425053 ", "psd");
- head2FileType.put("7B5C727466", "rtf");
- head2FileType.put("3C3F786D6C", "xml");
- head2FileType.put("68746D6C3E ", "html");
- head2FileType.put("44656C69766572792D646174", "eml");
- head2FileType.put("CFAD12FEC5FD746F ", "dbx");
- head2FileType.put("2142444E", "pst");
- head2FileType.put("D0CF11E0", "xls/doc");
- head2FileType.put("5374616E64617264204A", "mdb");
- head2FileType.put("FF575043", "wpd");
- head2FileType.put("252150532D41646F6265", "eps/ps");
- head2FileType.put("255044462D312E", "pdf");
- head2FileType.put("E3828596", "pwl");
- head2FileType.put("504B0304", "zip");
- head2FileType.put("52617221", "rar");
- head2FileType.put("57415645", "wav");
- head2FileType.put("41564920", "avi");
- head2FileType.put("2E7261FD", "ram");
- head2FileType.put("2E524D46", "rm");
- head2FileType.put("000001BA", "mpg");
- head2FileType.put("000001B3", "mpg");
- head2FileType.put("6D6F6F76", "mov");
- head2FileType.put("3026B2758E66CF11", "asf");
- head2FileType.put("4D546864", "mid");
- }
- private static String bytesToHexString(String fileName) throws IOException{
- FileInputStream fis = null;
- StringBuilder stringBuilder = new StringBuilder();
- try{
- fis = new FileInputStream(new File(fileName));
- byte[] b = new byte[4];
- fis.read(b, 0, b.length);
- for (int i = 0; i < b.length; i++) {
- int v = b[i] & 0xFF;
- String hv = Integer.toHexString(v);
- if (hv.length() < 2) {
- stringBuilder.append(0);
- }
- stringBuilder.append(hv);
- }
- }finally{
- if(fis != null)
- fis.close();
- }
- return stringBuilder.toString().toUpperCase();
- }
- public static String fileType(String fileName) throws IOException{
- String head = bytesToHexString(fileName);
- return head2FileType.get(head);
- }
- public static void main(String[] args) throws IOException {
- System.out.println(fileType("d://aaa.png"));
- }
- }