java上傳文件類型檢測


在進行文件上傳時,特別是向普通用戶開放文件上傳功能時,需要對上傳文件的格式進行控制,以防止黑客將病毒腳本上傳。單純的將文件名的類型進行截取的方式非常容易遭到破解,上傳者只需要將病毒改換文件名便可以完成上傳。

可以讀取文件的十六進制的文件頭,來判斷文件真正的格式。

讀取文件的二進制數據並將其轉換為十六進制時,同類型文件的文件頭數據是相同的,即使改變了其后綴,這個數據也不會改變

import java.io.*;
import java.util.HashMap;

public class GetFileType {

        // 緩存文件頭信息-文件頭信息
        public static final HashMap<String, String> mFileTypes = new HashMap<String, String>();
        static {
            mFileTypes.put("FFD8FFE0","jpg");
            mFileTypes.put("89504E47","png");
            mFileTypes.put("424DC6CC","bmp");
            mFileTypes.put("47494638","gif");
        }
        /**
     * 根據文件路徑獲取文件頭信息
     *
     * @param filePath 文件路徑
     * @return 文件頭信息
     */
    public static String getFileType(String filePath) {
        String type = getFileHeader(filePath);
        System.out.println(type);
        return mFileTypes.get(type);
    }

    /**
     * 根據文件路徑獲取文件頭信息
     *
     * @param filePath 文件路徑
     * @return 文件頭信息
     */
    public static String getFileHeader(String filePath) {
        FileInputStream is = null;
        String value = null;
        try {
            is = new FileInputStream(filePath);
            byte[] b = new byte[4];
            /*
             * int read() 從此輸入流中讀取一個數據字節。 int read(byte[] b) 從此輸入流中將最多 b.length
             * 個字節的數據讀入一個 byte 數組中。 int read(byte[] b, int off, int len)
             * 從此輸入流中將最多 len 個字節的數據讀入一個 byte 數組中。
             */
            is.read(b, 0, b.length);
            value = bytesToHexString(b);
        } catch (Exception e) {
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        return value;
    }

    /**
     * 將要讀取文件頭信息的文件的byte數組轉換成string類型表示
     *
     * @param src 要讀取文件頭信息的文件的byte數組
     * @return 文件頭信息
     */
    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++) {
            // 以十六進制(基數 16)無符號整數形式返回一個整數參數的字符串表示形式,並轉換為大寫
            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) {
        String path = "E:/file/2.png";
        String type = getFileType(path);
        System.out.println(type);
        path = "E:/file/timg.jpg";
        type = getFileType(path);
        System.out.println(type);
        path = "E:/file/bmp.bmp";
        type = getFileType(path);
        System.out.println(type);
        path = "E:/file/winter.gif";
        type = getFileType(path);
        System.out.println(type);
    }

}

運行輸出

89504E47
png
FFD8FFE0
jpg
424DC6CC
bmp
47494638
gif


免責聲明!

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



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