博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java通过文件头内容判断文件类型
阅读量:6813 次
发布时间:2019-06-26

本文共 5096 字,大约阅读时间需要 16 分钟。

转自:http://space.itpub.net/23071790/viewspace-691870

/** * 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),文件头:FFD8FFPNG (png),文件头:89504E47GIF (gif),文件头:47494638TIFF (tif),文件头:49492A00 Windows Bitmap (bmp),文件头:424D   */    }

解析:

用文件头判断。直接读取文件的前几个字节。常用文件的文件头如下:JPEG (jpg),文件头:FFD8FFPNG (png),文件头:89504E47GIF (gif),文件头:47494638TIFF (tif),文件头:49492A00 Windows Bitmap (bmp),文件头:424DCAD (dwg),文件头:41433130Adobe Photoshop (psd),文件头:38425053Rich Text Format (rtf),文件头:7B5C727466XML (xml),文件头:3C3F786D6CHTML (html),文件头:68746D6C3EEmail [thorough only] (eml),文件头:44656C69766572792D646174653AOutlook Express (dbx),文件头:CFAD12FEC5FD746F Outlook (pst),文件头:2142444E MS Word/Excel (xls.or.doc),文件头:D0CF11E0MS Access (mdb),文件头:5374616E64617264204AWordPerfect (wpd),文件头:FF575043Postscript. (eps.or.ps),文件头:252150532D41646F6265Adobe Acrobat (pdf),文件头:255044462D312EQuicken (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),文件头:000001B3Quicktime (mov),文件头:6D6F6F76 Windows Media (asf),文件头:3026B2758E66CF11 MIDI (mid),文件头:4D546864
GRAPHICS FILESAdobe 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 FILESText 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. formats00000000  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 <

 

转载地址:http://samwl.baihongyu.com/

你可能感兴趣的文章
“独立博客”为什么独立?
查看>>
Uber花了21亿元入驻上海自贸区 不叫优步叫雾博
查看>>
Java内存模型
查看>>
AppLinks使用详解
查看>>
JavaScript正则表达式19例(11)
查看>>
UNIX发展历史流程图
查看>>
负载均衡之LVS详解
查看>>
WP7实例篇之土豆搜索器(2)
查看>>
图解Cisco Packet Tracert之利用TFTP来升级路由器的IOS
查看>>
使用SDM配置基于IPsec 加密的GRE隧道
查看>>
Windows远程桌面及其相关问题
查看>>
Spring Security 学习之X.509认证
查看>>
nginx技术(4)nginx地址重写
查看>>
Discuss about PortableRemoteObject.narrow()
查看>>
用SHELL脚本自动化安装Nagios服务器端和客户端的
查看>>
System Center 2012 R2 CM系列之配置configuration manager防火墙设置
查看>>
我的CSS命名规则
查看>>
Configuration Manager 2012 R2系统需求
查看>>
OMF添加在线日志_OCP学习笔记(4)
查看>>
【Vue】详解Vue生命周期
查看>>