How to determine the type of the uploaded file

Source: Internet
Author: User
Determine with the file header. Directly read the first few bytes of the file. Common File Header: JPEG (JPG), file header: ffd8ff PNG (PNG), file header: 89504e47 GIF (GIF), file header: 47494638 tiff (TIF), file header: 4910000a00 Windows Bitmap (BMP), file header: Drawing 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: export outlook (PST), file header: 2142444e MS word/Excel (xls.or.doc), file header: d0cf11e0 MS access (MDB), file header: Export 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

 

package org.test;import java.io.FileInputStream;public class Test {    public static String bytesToHexString(byte[] src){            StringBuilder stringBuilder = new StringBuilder();            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();        }        /**     * @param args     */    public static void main(String[] args) throws Exception {        FileInputStream is = new FileInputStream("D:\\face.jpg");        byte[] b = new byte[3];        is.read(b, 0, b.length);        System.out.println(bytesToHexString(b));    }}

 

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.