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";
        }
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bridge Fish

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值