JAVA根據文件頭判斷文件類型(持續更新)


    舉個例子,一張jpg格式的圖片直接修改后綴名為png也可以打開,但當一些方法需要調用后綴為png格式圖片的時候,再使用這個文件,則會出現報錯。是因為這張圖片的本質沒有變,他還是個png格式的文件。

  那么,該如何去掉文件名的偽裝,拿到文件的真實格式呢?

  使用java.io包中的FileInputStream類。通過讀取原始字節流,根據字節流頭部獲取到文件格式。從而判斷一個文件是否被修改過。

  下面提供一個可行方法 

// file path String path = ""; 
File file = new File(path); 
FileInputStream fs = new FileInputStream(file); 
// 具體需要多大的數組要根據具體文件來判斷,這里先定義大小為10,大部分文件定義3夠用 
byte[] b = new byte[10]; 
fs.read(b,0,b.length);
String suffix = checkFileType(bytesToHexString(b));
 

  這里給出一個講數組轉換成為十六進制字符串的方法

public static String bytesToHexString(byte[] b){ 
    StringBuilder stringBuilder = new StringBuilder(); 
    if (b == null || b.length <= 0) { 
        return null; 
    }     
    for (int i = 0; i < b.length; i++) { 
int v = b[i] & 0xFF; 
        String hv = Integer.toHexString(v); 
        if (hv.length() < 2) { 
            stringBuilder.append(0); 
        } 
        stringBuilder.append(hv); 
    } 
    return stringBuilder.toString(); 
}

再定義一個枚舉類 點擊查看代碼

public static String checkFileType(String x) {
        switch (x) {
            case "255044462d312e360d25":
                return "pdf";
            case "e69292e58f91e8bebee6":
                return "txt";
            case "d0cf11e0a1b11ae10000":
                return "doc,ppt,xls";
            case "504b0304140006000800":
                return "docx,xlsx";
            case "504b03040a0000000000":
                return "pptx,jar";
            case "89504e470d0a1a0a0000":
                return "png";
            case "ffd8ffe000104a464946":
                return "jpg,jpeg";
            case "47494638396126026f01":
                return "gif";
            case "49492a00227105008037":
                return "tif,tiff";
            //16位色圖
            case "424d228c010000000000":
                return "bmp";
            //24位色圖
            case "424d8240090000000000":
                return "bmp";
            //256位色圖
            case "424d8e1be30000000000":
                return "bmp";
            case "3c21444f435459504520":
                return "html";
            case "526172211a0700cf9073":
                return "rar";
            case "504b0304140000000800":
                return "zip";
            case "235468697320636f6e66":
                return "ini";
            case "4d5a9000030000000400":
                return "exe";
            case "49443303000000002176":
                return "mp3";
            case "49443303000000034839":
                return "mp3";
            case "00000020667479706973":
                return "mp4";
            case "000001ba210001000180":
                return "mpg";
            case "3026b2758e66cf11a6d9":
                return "wmv,asf";
            case "52494646d07d60074156":
                return "avi";
            case "464c5601050000000900":
                return "flv,f4v";
            case "4d546864000000060001":
                return "mid,midi";
            default:
                return "0000";
        }
}
View Code

后續還會補充各種各樣的文件類型。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM