ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.util.HashMap;ImportJava.util.Map.Entry;/*** @Description based on the file header to determine the file type *@authorLJ * @Version v1.0*/ Public classGetfiletypebyhead { Public Static voidMain (string[] args)throwsException {String FileType= Getfiletype ("E:\\vdsfdfadsafsf.docx"); System.out.println (FileType); } //Cache file header information-File header information Public Static Finalhashmap<string, string> mfiletypes =NewHashmap<string, string>(); Static { //ImagesMfiletypes.put ("ffd8ff", "JPG"); Mfiletypes.put ("89504E47", "PNG"); Mfiletypes.put ("47494638", "GIF"); Mfiletypes.put ("49492a00", "TIF"); Mfiletypes.put ("424D", "BMP"); //Mfiletypes.put ("41433130", "DWG");//CADMfiletypes.put ("38425053", "PSD"); Mfiletypes.put ("7b5c727466", "RTF");//DiaryMfiletypes.put ("3c3f786d6c", "xml"); Mfiletypes.put ("68746d6c3e", "HTML"); Mfiletypes.put ("44656c69766572792d646174653a", "eml");//MailMfiletypes.put ("D0cf11e0", "Doc"); Mfiletypes.put ("5374616e64617264204a", "MDB"); Mfiletypes.put ("252150532d41646f6265", "PS"); Mfiletypes.put ("255044462d312e", "PDF"); Mfiletypes.put ("504b03040a00000000008", "docx"); Mfiletypes.put ("504b0304", "zip");//Zip compressed fileMfiletypes.put ("52617221", "rar"); Mfiletypes.put ("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"); } /*** Get file type according to file path * *@paramFilePath * File path *@returnFile Type*/ Public Staticstring Getfiletype (String filePath) {String value=GetFileHeader (FilePath); String result= ""; for(Entry<string, string>Entry:mFileTypes.entrySet ()) { if(Value.startswith (Entry.getkey ())) {result=Entry.getvalue (); } } returnresult; } /*** Get file header information according to file path * *@paramFilePath * File path *@returnFile header information*/ Public Staticstring GetFileHeader (String filePath) {FileInputStream is=NULL; String value= ""; Try{ is=NewFileInputStream (FilePath); byte[] B =New byte[20]; Is.read (b,0, b.length); Value=bytestohexstring (b); } Catch(Exception e) {}finally { if(NULL!=is ) { Try{is.close (); } Catch(IOException e) {}}} returnvalue; } /*** Convert a byte array of the file that will read the header information to a string type representing * *@paramsrc * A byte array of files to read the header information of the file *@returnFile header hex Information*/ Private StaticString bytestohexstring (byte[] src) {StringBuilder Builder=NewStringBuilder (); if(src = =NULL|| Src.length <= 0) { return NULL; } String HV; for(inti = 0; i < src.length; i++) { //returns the string representation of an integer parameter as an unsigned integer of 16 (Radix 16) and converts it to uppercaseHV = Integer.tohexstring (Src[i] & 0xFF). toUpperCase (); if(Hv.length () < 2) {builder.append (0); } builder.append (HV); } System.out.println ("HexString:" +builder.tostring ()); returnbuilder.tostring (); }}
Java uses file headers to determine file types