JAVA判断图片真实格式的方法

发布时间 2023-11-29 10:20:26作者: 迷糊桃

判断图片真实格式的方法,文件格式不是看后缀名,而是看文件头的定义

 public class ImgUtil {
    public static String imgType(InputStream inputStream) throws IOException {
        // 读取文件前几位
        byte[] fileHeader = new byte[4];
        int read = inputStream.read(fileHeader, 0, fileHeader.length);
        inputStream.close();
        
        // 转为十六进制字符串
        String header = ByteUtil.bytes2Hex(fileHeader);
        
        if (header.contains("FFD8FF")) {
            return "jpg";
        } else if (header.contains("89504E47")) {
            return "png";
        } else if (header.contains("47494638")) {
            return "gif";
        } else if (header.contains("424D")) {
            return "bmp";
        } else if (header.contains("52494646")) {
            return "webp";
        } else if (header.contains("49492A00")) {
            return "tif";
        } else {
            return "unknown";
        }
 
    }
 
    public static String bytes2Hex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(b & 0xff);
            sb.append(hex.length() == 2 ? hex : ("0"+hex));
        }
        return sb.toString();
    }
 
    public static void main(String[] args) throws IOException {
        String path ="D:\\panda.webp";
        File file = new File(path);
        System.out.println(ImgUtil.imgType(new FileInputStream(file)));
    }
}