Android中Java根據檔案頭擷取檔案類型

來源:互聯網
上載者:User
前面貼過Android系統內部的MediaFile類來擷取檔案類型的辦法,這個類主要是根據檔案的副檔名來判斷,其準確性不是很好。具體可查看Android系統使用MediaFile類判斷音頻檔案類型。其實,擷取檔案類型最好的辦法便是根據檔案頭資訊來判斷。下面貼出相關代碼:public class FileType {      public static final HashMap<String, String> mFileTypes = new HashMap<String, String>();    static {        //images        mFileTypes.put("FFD8FF", "jpg");        mFileTypes.put("89504E47", "png");        mFileTypes.put("47494638", "gif");        mFileTypes.put("49492A00", "tif");        mFileTypes.put("424D", "bmp");        //        mFileTypes.put("41433130", "dwg"); //CAD        mFileTypes.put("38425053", "psd");        mFileTypes.put("7B5C727466", "rtf"); //日記本        mFileTypes.put("3C3F786D6C", "xml");        mFileTypes.put("68746D6C3E", "html");        mFileTypes.put("44656C69766572792D646174653A", "eml"); //郵件        mFileTypes.put("D0CF11E0", "doc");        mFileTypes.put("5374616E64617264204A", "mdb");        mFileTypes.put("252150532D41646F6265", "ps");        mFileTypes.put("255044462D312E", "pdf");        mFileTypes.put("504B0304", "zip");        mFileTypes.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");        mFileTypes.put("", "");        mFileTypes.put("", "");    }    public static String getFileType(String filePath) {        return mFileTypes.get(getFileHeader(filePath));    }    //擷取檔案頭資訊    public static String getFileHeader(String filePath) {        FileInputStream is = null;        String value = null;        try {            is = new FileInputStream(filePath);            byte[] b = new byte[3];              is.read(b, 0, b.length);            value = bytesToHexString(b);        } catch (Exception e) {        } finally {            if(null != is) {                try {                    is.close();                } catch (IOException e) {}            }        }        return value;    }    private static String bytesToHexString(byte[] src){              StringBuilder builder = new StringBuilder();              if (src == null || src.length <= 0) {                  return null;              }         String hv;         for (int i = 0; i < src.length; i++) {                  hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();                  if (hv.length() < 2) {                      builder.append(0);                  }                  builder.append(hv);         }         return builder.toString();          }      public static void main(String[] args) throws Exception {          final String fileType = getFileType("D:/apache-tomcat-6.0.35.tar.gz");        System.out.println(fileType);      }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.