背景
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 ==