美文网首页
判断图片格式

判断图片格式

作者: 一劍 | 来源:发表于2024-05-30 14:56 被阅读0次

背景:图片格式根据后缀无法准确识别,导致一些需要明确限制图片格式的功能,无法做到限制。

工具:使用 apache 提供的 图片工具类,可以去大体上识别出来

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-imaging</artifactId>
        <version>1.0-alpha2</version>
    </dependency>

使用:很简单,直接调用即可

public static String getImageFormat2(InputStream inputStream) throws IOException, ImageReadException {
    ImageFormat imageFormat = Imaging.guessFormat(new ByteSourceInputStream(inputStream, "文件名"));
    return imageFormat.getName();
}

使用了一些图片,测试了其格式判断,基本上是没问题

相关文章

网友评论

      本文标题:判断图片格式

      本文链接:https://www.haomeiwen.com/subject/cshrqjtx.html