Determine the file type using the file header

Source: Internet
Author: User

When uploading files, you often need to determine the file type, such as examples and documents. The common practice is to directly determine the file suffix, in order to prevent various attacks, literary and art youths also use the file header information to determine the file type.

The principle is simple: you can use the file header to directly read the first two bytes of the file.

1 public class fileutil {2/** 3 * byte array to a hexadecimal string 4*5 * @ Param SRC 6 * @ return 7 */8 Private Static string bytestohexstring (byte [] SRC) {9 stringbuilder = new stringbuilder (); 10 if (src = NULL | SRC. length <= 0) {11 return NULL; 12} 13 for (INT I = 0; I <SRC. length; I ++) {14 // returns the string representation of an integer parameter in hexadecimal notation (base 16, and converts it to uppercase 15 string HV = integer. tohexstring (SRC [I] & 0xff ). touppe Rcase (); 16 if (HV. length () <2) {17 stringbuilder. append (0); 18} 19 stringbuilder. append (HV); 20} 21 return stringbuilder. tostring (); 22} 23 24/** 25 * obtain the file header information based on the file path 26*27 * @ Param filepath28 * file path 29 * @ return file header information 30 */31 public static string getfiletype (string filepath) {32 string bytes = getfileheader (filepath); 33 If (Bytes! = NULL) {34 if (bytes. contains ("ffd8ff") {35 return "jpg"; 36} else if (bytes. contains ("89504e47") {37 return "PNG"; 38} else if (bytes. contains ("47494638") {39 return "GIF"; 40} else if (bytes. contains ("49347a00") {41 return "TIF"; 42} else if (bytes. contains ("mongod") {43 return "BMP"; 44} 45} 46 return NULL; 47} 48 49 50/** 51 * obtain the file header information based on the file path 52*53 * @ Param filepath54 * file path 55 * @ return file header information 56 */57 public static string getfileheader (string filepath) {58 fileinputstream is = NULL; 59 string value = NULL; 60 try {61 is = new fileinputstream (filepath ); 62 byte [] B = new byte [4]; 63 is. read (B, 0, B. length); 64 value = bytestohexstring (B); 65} catch (exception e) {66} finally {67 If (null! = Is) {68 try {69 is. close (); 70} catch (ioexception e) {71} 72} 73} 74 return value; 75} 76 77 public static void main (string [] ARGs) throws exception {78 system. out. println (getfiletype ("F: \ old file \ Forum profile picture data collection \ unnamed .bmp"); 79} 80}

The client JSP call instance is added here.

String ext = FileUtil.getFileType(srcFilePath);if(app.getFileExts()!=null && !app.getFileExts().contains(ext)){      File invalidFile = new File(srcFilePath);      if(invalidFile.isFile()) invalidFile.delete();      System.out.println("Unsupport file type: "+ext+" in tx:"+tx.getTransactionId());      throw new Exception("Unsupport file type: "+ext+" in tx:"+tx.getTransactionId());}

By the way, paste various common file headers

JPEG (JPG), file header: ffd8ff PNG (PNG), file header: 89504e47 GIF (GIF), file header: 47494638 tiff (TIF), file header: 49347a00 Windows Bitmap (BMP), file header: development d cad (DWG), file header: 41433130 Adobe Photoshop (PSD), file header: 38425053 Rich Text Format (RTF), file header: 7b5c727466 XML (XML), file header: 3c3f786d6c HTML (HTML), file header: 68746d6c3e email [thorough only] (EML), file header: Outlook Express (DBX), file header: cfad12fec5fd746f Outlook (PST), file header: 2142444e MS word/Excel (xls.or.doc), file header: d0cf11e0 MS access (MDB), file header: 5374616e64617450204a Wordperfect (WPD), file header: ff575043 postscript (EPS. or. PS), file header: Adobe Acrobat (PDF), file header: 255044462d312e quicken (qdf), file header: ac9ebd8f Windows Password (pwl), file header: e3828596 ZIP Archive (ZIP), file header: 504b0304 RAR Archive (RAR), file header: 52617221 wave (WAV), file header: 57415645 AVI (AVI), file header: 41564920 Real Audio (RAM), file header: 2e00001fd Real Media (RM), file header: 2e524d46 MPEG (MPG), file header: 000001ba MPEG (MPG), file header: 000001b3 QuickTime (mov), file header: 6d6f6f76 Windows Media (ASF ), file Header: 3026b2758e66cf11 Midi (MID), file header: 4d546864

Refer:

Determine the file type using the file header

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.