Java determines file type (suffix name) based on file stream

Source: Internet
Author: User
Tags rar

ImportJava.io.FileInputStream; Public classfiletype{ Public StaticString bytestohexstring (byte[] src) {StringBuilder StringBuilder=NewStringBuilder (); if(src = =NULL|| Src.length <= 0) {            return NULL; }         for(inti = 0; i < src.length; i++) {            intv = src[i] & 0xFF; String HV=integer.tohexstring (v); if(Hv.length () < 2) {stringbuilder.append (0);        } stringbuilder.append (HV); }        returnstringbuilder.tostring (); }    /**     * @paramargs*/     Public Static voidMain (string[] args)throwsException {FileInputStream is=NewFileInputStream ("f:\\ album \ \ Picture \\03.jpg"); byte[] B =New byte[3]; Is.read (b,0, b.length); String XXX=bytestohexstring (b); XXX=xxx.touppercase (); System.out.println ("Header file is:" +xxx); String ooo=typedict.checktype (XXX); System.out.println (The suffix name is: "+ooo); }    }
 Public classtypedict {/** File headers for common files are as follows: (previous six-digit) JPEG (jpg), file header: ffd8ff png (PNG), file header: 89504E47 gif (GIF), file header: 47494638 TIFF (TIF), file header: 49492a00 Windows  Bitmap (BMP), file header: 424D 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: 44656c69766572792d646174653a 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: 5374616e64617264204a WordPerfect (WPD), file header: FF575043 Postscript (eps.or.ps), file header: 252150532d41646f6265 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: 2E7261FD 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*/     Public Staticstring Checktype (string xxxx) {Switch(xxxx) { Case"FFD8FF":return"JPG";  Case"89504E":return"PNG";  Case"474946":return"Jif"; default:return"0000"; }    }}

Java determines file type (suffix name) based on file stream

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.