补充通过字节流判断文件类型 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.youkuaiyun.com/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 == 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值