补充通过字节流判断文件类型 URLConnection判断Content-Type

补充通过字节流判断文件类型 URLConnection判断Content-Type

背景

webp 文件是一种用于图像压缩的格式,通常用于图像处理和网络传输。 wav 文件是一种用于存储音频文件的格式,通常用于音频处理和播放。

文件扩展名: webp 图像文件通常使用 .webp 扩展名。 wav 音频文件通常使用 .wav 扩展名。

MIME 类型: webp 图像格式的 MIME 类型通常为 image/webp。 wav 音频格式的 MIME 类型通常为
audio/wav。

Android API 33 URLConnection系统源码类型判断遗漏

webp 文件格式,通常以 RIFF 或 WEBP 作为文件头部
wav 文件格式,通常包含特定的头部标识符(RIFF)

以上两种类型都是以(RIFF)为头部标识,将无法区分具体类型,而系统源码以"audio/x-wav"判定。

方案

头部标识都是RIFF,通过第9~12位区分具体类型,判断为"WEBP"还是"WAVE"

附:ASCII 对照表:https://blog.csdn.net/sunyctf/article/details/131397591

例子:一个.webp文件格式的字节流前16位ASCll码对应如下

RIFFœ(EOT)(NUL)(NUL)WEBPVP8X
在这里插入图片描述

代码

补充判断webp的类型

public static String guessContentTypeFromStream(InputStream is) throws IOException {
        if (!is.markSupported()) {
            return null;
        } else {
            is.mark(16);
            int c1 = is.read();
            int c2 = is.read();
            int c3 = is.read();
            int c4 = is.read();
            int c5 = is.read();
            int c6 = is.read();
            int c7 = is.read();
            int c8 = is.read();
            int c9 = is.read();
            int c10 = is.read();
            int c11 = is.read();
            int c12 = is.read();
            int c13 = is.read();
            int c14 = is.read();
            int c15 = is.read();
            int c16 = is.read();
            is.reset();
            if (c1 == 202 && c2 == 254 && c3 == 186 && c4 == 190) {
                return "application/java-vm";
            } else if (c1 == 172 && c2 == 237) {
                return "application/x-java-serialized-object";
            } else {
                if (c1 == 60) {
                    if (c2 == 33 || c2 == 104 && (c3 == 116 && c4 == 109 && c5 == 108 || c3 == 101 && c4 == 97 && c5 == 100) || c2 == 98 && c3 == 111 && c4 == 100 && c5 == 121 || c2 == 72 && (c3 == 84 && c4 == 77 && c5 == 76 || c3 == 69 && c4 == 65 && c5 == 68) || c2 == 66 && c3 == 79 && c4 == 68 && c5 == 89) {
                        return "text/html";
                    }

                    if (c2 == 63 && c3 == 120 && c4 == 109 && c5 == 108 && c6 == 32) {
                        return "application/xml";
                    }
                }

                if (c1 == 239 && c2 == 187 && c3 == 191 && c4 == 60 && c5 == 63 && c6 == 120) {
                    return "application/xml";
                } else if (c1 == 254 && c2 == 255 && c3 == 0 && c4 == 60 && c5 == 0 && c6 == 63 && c7 == 0 && c8 == 120) {
                    return "application/xml";
                } else if (c1 == 255 && c2 == 254 && c3 == 60 && c4 == 0 && c5 == 63 && c6 == 0 && c7 == 120 && c8 == 0) {
                    return "application/xml";
                } else if (c1 == 0 && c2 == 0 && c3 == 254 && c4 == 255 && c5 == 0 && c6 == 0 && c7 == 0 && c8 == 60 && c9 == 0 && c10 == 0 && c11 == 0 && c12 == 63 && c13 == 0 && c14 == 0 && c15 == 0 && c16 == 120) {
                    return "application/xml";
                } else if (c1 == 255 && c2 == 254 && c3 == 0 && c4 == 0 && c5 == 60 && c6 == 0 && c7 == 0 && c8 == 0 && c9 == 63 && c10 == 0 && c11 == 0 && c12 == 0 && c13 == 120 && c14 == 0 && c15 == 0 && c16 == 0) {
                    return "application/xml";
                } else if (c1 == 71 && c2 == 73 && c3 == 70 && c4 == 56) {
                    return "image/gif";
                } else if (c1 == 35 && c2 == 100 && c3 == 101 && c4 == 102) {
                    return "image/x-bitmap";
                } else if (c1 == 33 && c2 == 32 && c3 == 88 && c4 == 80 && c5 == 77 && c6 == 50) {
                    return "image/x-pixmap";
                } else if (c1 == 137 && c2 == 80 && c3 == 78 && c4 == 71 && c5 == 13 && c6 == 10 && c7 == 26 && c8 == 10) {
                    return "image/png";
                } else {
                    if (c1 == 255 && c2 == 216 && c3 == 255) {
                        if (c4 == 224 || c4 == 238) {
                            return "image/jpeg";
                        }

                        if (c4 == 225 && c7 == 69 && c8 == 120 && c9 == 105 && c10 == 102 && c11 == 0) {
                            return "image/jpeg";
                        }
                    }

                    if ((c1 != 73 || c2 != 73 || c3 != 42 || c4 != 0) && (c1 != 77 || c2 != 77 || c3 != 0 || c4 != 42)) {
                        if (c1 == 208 && c2 == 207 && c3 == 17 && c4 == 224 && c5 == 161 && c6 == 177 && c7 == 26 && c8 == 225 && checkfpx(is)) {
                            return "image/vnd.fpx";
                        } else if (c1 == 46 && c2 == 115 && c3 == 110 && c4 == 100) {
                            return "audio/basic";
                        } else if (c1 == 100 && c2 == 110 && c3 == 115 && c4 == 46) {
                            return "audio/basic";
                        } else if (c1 == 82 && c2 == 73 && c3 == 70 && c4 == 70) {
                            //补充区分webp或wave
                            if (c9 == 87 && c10 == 69 && c11 == 66 && c12 == 80) {
                                return "image/webp";
                            } else {
                                return "audio/x-wav";
                            }
                        } else {
                            return null;
                        }
                    } else {
                        return "image/tiff";
                    }
                }
            }
        }
    }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值