java通过文件头内容判断文件类型

zhuxi188177 贡献于2011-07-13

作者 wzhen  创建于2011-07-13 04:53:00   修改者wzhen  修改于2011-07-13 04:54:00字数4842

文档摘要:
关键词:

java通过文件头内容判断文件类型 /** * byte数组转换成16进制字符串 * @param src * @return */ public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * 根据文件流读取图片文件真实类型 * @param is * @return */ public static String getTypeByStream(FileInputStream is){ byte[] b = new byte[4]; try { is.read(b, 0, b.length); } catch (IOException e) { e.printStackTrace(); } String type = bytesToHexString(b).toUpperCase(); if(type.contains("FFD8FF")){ return "jpg"; }else if(type.contains("89504E47")){ return "png"; }else if(type.contains("47494638")){ return "gif"; }else if(type.contains("49492A00")){ return "tif"; }else if(type.contains("424D")){ return "bmp"; } return type; } public static void main(String[] args) throws Exception { // String src = "D:/workspace//8129.jpg"; // String src = "D:/workspace//temp/1.gif"; String src = "D:/workspace//temp/2.bmp"; FileInputStream is = new FileInputStream(src); // byte[] b = new byte[4]; // is.read(b, 0, b.length); // System.out.println(bytesToHexString(b)); String type = getTypeByStream(is); System.out.println(type); /* * JPEG (jpg),文件头:FFD8FF PNG (png),文件头:89504E47 GIF (gif),文件头:47494638 TIFF (tif),文件头:49492A00 Windows Bitmap (bmp),文件头:424D */ } ================================== 用文件头判断。直接读取文件的前几个字节。 常用文件的文件头如下: 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 GRAPHICS FILES Adobe Photoshop File (.psd) 00000000 38 42 50 53 00 01 00 00 00 00 00 00 00 04 00 00 |8BPS............| 00000010 0b 71 00 00 10 dd 00 08 00 03 00 00 00 00 00 00 |.q...?..........| 00000020 6f c4 38 42 49 4d 04 04 00 00 00 00 00 07 1c 02 |o?8BIM..........| JPEG image (.jpg) 00000000 ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 |????..JFIF.....H| 00000010 00 48 00 00 ff db 00 43 00 06 04 05 06 05 04 06 |.H..??.C........| 00000020 06 05 06 07 07 06 08 0a 10 0a 0a 09 09 0a 14 0e |................| PNG image (.png) 00000000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 |.PNG........IHDR| 00000010 00 00 03 20 00 00 02 58 08 06 00 00 00 9a 76 82 |... ...X......v.| 00000020 70 00 00 0c d9 69 43 43 50 69 63 63 00 00 78 da |p...?iCCPicc..x?| GIF image (.gif) 00000000 47 49 46 38 39 61 10 00 10 00 b3 0d 00 3f 3f 3f |GIF89a....?..???| 00000010 bf bf bf 2a 2a 2a 55 55 55 7f 7f 7f 15 15 15 40 |???***UUU......@| 00000020 40 40 60 60 60 c0 c0 c0 2f 2f 2f 90 90 90 ff ff |@@```???///...??| Adobe Illustrator File (.ai) 00000000 25 50 44 46 2d 31 2e 34 0d 25 e2 e3 cf d3 0d 0a |%PDF-1.4.%????..| 00000010 31 20 30 20 6f 62 6a 3c 3c 2f 50 61 67 65 73 20 |1 0 obj<?..W.?.| AIFF file (.aif) 00000000 46 4f 52 4d 00 2a ef cc 41 49 46 46 43 4f 4d 54 |FORM.*??AIFFCOMT| 00000010 00 00 01 c2 00 01 00 00 00 00 00 00 00 12 43 72 |...?..........Cr| 00000020 65 61 74 6f 72 3a 20 4c 6f 67 69 63 20 50 72 6f |eator: Logic Pro| TEXT FILES Text file (often .txt, but not always) 00000000 48 69 20 65 76 65 72 79 6f 6e 65 2c 0a 0a 48 65 |Hi everyone,..He| 00000010 72 65 20 61 72 65 20 73 6f 6d 65 20 63 68 61 6e |re are some chan| 00000020 67 65 73 20 74 68 61 74 20 77 69 6c 6c 20 68 61 |ges that will ha| Microsoft Word/Office (.doc, .xls) 00000000d0 cf 11 e0 a1 b1 1a e100 00 00 00 00 00 00 00 |??.....?........| 00000010 00 00 00 00 00 00 00 00 3e 00 03 00 fe ff 09 00 |........>...??..| 00000020 06 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 |................| Adobe PDF (.pdf) - Very similar to Adobe Illustrator and other Postscript. formats 00000000 25 50 44 46 2d 31 2e 34 0d 25 e2 e3 cf d3 0d 0a |%PDF-1.4.%????..| 00000010 36 20 30 20 6f 62 6a 20 3c 3c 2f 4c 69 6e 65 61 |6 0 obj <

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 15 金币 [ 分享文档获得金币 ] 4 人已下载

下载文档