code show as below:
Import a java.io.FileInputStream; Import java.io.IOException; Import the java.util.HashMap; / ** * <P> * Class Description: Gets the header information and determining * | - header file is located at the beginning of period undertake certain tasks of data, usually at the beginning of the section. * | - header file as a carrier comprising a performance function, data interface declaration, a statement for storing the program (Declaration), and the definition file storing programs for realizing the (implementation). * | - To solve the problem when the user to upload files to determine the file type on the server side, it is a way to get the file header directly read the first few bytes of the file to determine whether uploading files conform to the format. * / Public class CheckFileFormatUtil { // cache file header information - file header information Private static Final the HashMap <String, String> = mFileTypes new new the HashMap <String, String> (); static { // images mFileTypes.put("FFD8FF", "jpg"); mFileTypes.put("89504E47", "png"); mFileTypes.put("47494638", "gif"); mFileTypes.put("49492A00", "tif"); mFileTypes.put("424D", "bmp"); // mFileTypes.put("41433130", "dwg"); // CAD mFileTypes.put("38425053", "psd"); mFileTypes.put("7B5C727466", "rtf"); // 日记本 mFileTypes.put("3C3F786D6C", "xml"); mFileTypes.put("68746D6C3E", "html"); mFileTypes.put("44656C69766572792D646174653A", "eml"); // 邮件 mFileTypes.put("D0CF11E0", "doc"); mFileTypes.put("D0CF11E0", "xls");//excel2003版本文件 mFileTypes.put("5374616E64617264204A", "mdb"); mFileTypes.put("252150532D41646F6265", "ps"); mFileTypes.put("255044462D312E", "pdf"); mFileTypes.put("504B0304", "docx"); mFileTypes.put("504B0304", "xlsx"); mFileTypes.put ( "52,617,221", "RAR"Excel2007 or later file//); mFileTypes.put(filePath file path"57415645", "wav"); mFileTypes.put("41564920", "avi"); mFileTypes.put("2E524D46", "rm"); mFileTypes.put("000001BA", "mpg"); mFileTypes.put("000001B3", "mpg"); mFileTypes.put("6D6F6F76", "mov"); mFileTypes.put("3026B2758E66CF11", "asf"); mFileTypes.put("4D546864", "mid"); mFileTypes.put("1F8B08", "gz"); header information@return *@param */ ** } * @author WLx * <P> * Method Description: acquiring header information according to the file path * / public static String getFileType (String filePath) { // System.out.println (getFileHeader (filePath)); // the System .out.println (mFileTypes.get (getFileHeader (filePath))); return mFileTypes.get (getFileHeader (filePath)); } / ** * @param filePath file path * @return header information * @author WLx * <P > * method description: acquiring header information according to the file path * / public staticString getFileHeader (String filePath) { the FileInputStream IS = null ; String value = null ; the try { IS = new new the FileInputStream (filePath); byte [] B = new new byte [. 4 ]; / * * int Read () from this input stream is read take a data byte. int read (byte [] b) from this input stream up to b.length * bytes of data read into a byte array. Read int (byte [] B, OFF int, int len) * from this input stream up to len bytes of read data into a byte array. * / Is.read (B, 0 = , to b.length); value bytesToHexString (B); } the catch (Exception E) { e.printStackTrace (); } the finally { IF ( null ! = IS) { the try { is.close (); } the catch (IOException E) { e.printStackTrace () ; } } } return value; } / ** * @param byte array to read the header information of the file the src * @return header information * @authorWLx * <P> * Method Description: byte array to be read, the file header information of the file into a string type represents * / Private static String bytesToHexString ( byte [] the src) { the StringBuilder Builder = new new the StringBuilder (); IF (the src = = null || src.length <= 0 ) { return null ; } string HV; for ( byte ASRC: the src) { // returns a string parameter integer in hexadecimal (base 16) represents unsigned integer form, convert to uppercase and HV = Integer.toHexString (ASRC & 0xFF ) .toUpperCase (); IF (hv.length() < 2) { builder.append(0); } builder.append(hv); } // System.out.println(builder.toString()); return builder.toString(); } public static void main(String[] args) throws Exception { final String fileType = getFileType("E:\\项目管理清单.xlsx"); System.out.println(fileType); System.out.println(getFileHeader("E:\\项目管理清单.xlsx")); } }
Appendix: Magic number Profile
Many types of files, a few bytes of its initial content is fixed (or intentionally filled, or is this way).
The contents of these bytes of the file type may be determined, so the contents of these bytes are called magic numbers (magic number).
JPEG jpg;jpeg 0xFFD8FF PNG png 0x89504E470D0A1A0A GIF gif GIF8 TIFF tif;tiff 0x49492A00 TIFF tif;tiff 0x4D4D002A AOL ART art 0x4A47040E000000 AOL ART art 0x4A47030E000000 PC Paintbrush pcx 0x0A050108 Graphics Metafile wmf 0xD7CDC69A Graphics Metafile wmf 0x01000900 Graphics Metafile wmf 0x02000900 Enhanced Metafile emf 0x0100000058000000 CAD dwg 0x41433130 Outlook Express dbx 0xCFAD12FE MS Office/OLE2 doc;xls;dot;ppt;xla;ppa;pps;pot;msi;sdw;db 0xD0CF11E0A1B11AE1 WordPerfect wpd 0xFF575043 Quicken qdf 0xAC9EBD8F QuickBooks Backup qbb 0x458600000600 Sage sly.or.srt.or.slt;sly;srt;slt 0x53520100 Lotus WordPro v9 lwp 0x576F726450726F Lotus 123 v9 123 0x00001A00051004 Lotus 123 v5 wk4 0x00001A0002100400 Lotus 123 v3 wk3 0x00001A0000100400 Lotus 123 v1 wk1 0x2000604060 Windows Password pwl 0xE3828596 ZIP Archive zip;jar 0x504B0304 ZIP Archive (outdated) zip 0x504B3030 GZ Archive gz;tgz 0x1F8B08 ARJ Archive arj 0x60EA MPEG mpg;mpeg 0x000001BA MPEG mpg;mpeg 0x000001B3 Windows Media asf 0x3026B2758E66CF11 ELF Executable elf;; 0x7F454C4601010100 Java-Class class 0xCAFEBABE