To determine the file type in Java streaming mode

Source: Internet
Author: User
Tags rar

This method can only be effective within a limited range. It's not balm.

Like what

The image type judgment, the audio file format judgment, the video file format judgment and so on this affirmation is 2 binary and the professional very strong file type judgment.

The full version of the code is given below

The first is the file type fetch

Package org.filetype;/** * File type fetch */public enum filetype {/** * jepg. */jpeg ("ffd8ff"),/** * PNG. */png ("89504E47"),/** * Gif. */gif ("47494638"),/** * TIFF. */tiff ("49492a00"),/** * Windows Bitmap. */bmp ("424D"),/** * CAD. */DWG ("41433130"),/** * Adobe Photoshop. */PSD ("38425053"),/** * Rich Text Format. */rtf ("7b5c727466"),/** * XML. */xml ("3c3f786d6c"),/** * HTML. */html ("68746d6c3e"),/** * Email [thorough only]. */EML ("44656c69766572792d646174653a"),/** * Outlook Express. */dbx ("cfad12fec5fd746f"),/** * Outlook (PST). */pst ("2142444E"),/** * MS word/excel. */xls_doc ("D0cf11e0"),/** * MS Access. */mdb ("5374616e64617264204a"),/** * WordPerfect. */WPD ("FF575043"),/** * Postscript. */eps ("252150532d41646f6265"),/** * Adobe Acrobat. */pdf ("255044462d312e"),/** * Quicken. */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). */mpg ("000001BA"),/** * Quicktime. */mov ("6d6f6f76"),/** * Windows Media. */ASF ("3026b2758e66cf11"),/** * MIDI. */mid ("4d546864");p rivate String value = "";/** * Constructor. * * @param type */private FileType (String value) {this.value = value;} Public String GetValue () {return value;} public void SetValue (String value) {this.value = value;}}

Then the type judgment core class

Package Org.filetype;import Java.io.fileinputstream;import Java.io.ioexception;import java.io.InputStream;/** *  File type Judgment Class */public final class Filetypejudge {/** * Constructor */private Filetypejudge () {}/** * convert header to 16 binary string * * @param Native byte * @return 16 binary string */private static string bytestohexstring (byte[] src) {StringBuilder StringBuilder = new S           Tringbuilder ();           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 (); }/** * Get file Header * * @param filePath file path * @return file header * @throws ioexception */private static String getfilecontent (Strin G FilePath) throws IOException {byte[] b = new Byte[28];inputstream inputStream = null;try {inputstream = new FileInputStream (FilePath); Inputstream.read (b, 0,);} catch (IOException e) {E.print StackTrace (); throw e;} finally {if (InputStream! = null) {try {inputstream.close ()} catch (IOException e) {e.printstacktrace (); throw e;}}} return bytestohexstring (b);}  /** * Determine file type * * @param filePath file path * @return file type */public static FileType getType (String filePath) throws IOException {String Filehead = getfilecontent (FilePath), if (Filehead = = NULL | | filehead.length () = = 0) {return null;} Filehead = Filehead.touppercase (); filetype[] FileTypes = Filetype.values (); for (FileType type:filetypes) {if (Filehead.startswith (Type.getvalue ())) { return type;}} return null;}}

Finally, the test class

Package Org.filetype;public class Test {/** * @param args */public static void Main (String args[]) throws Exception {Syste M.out.println (Filetypejudge.gettype ("C:\\eclipse-jee-helios-win32.zip"));}}

  

To determine the file type in Java streaming mode

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.