根據文件的後綴名識別文件類型並不許確,能夠使用文件的頭信息進行識別:
如下是各種文件的頭:
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
檢測文件類型的代碼以下: html
- 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"));
- }
- }