背景:圖片格式根據(jù)后綴無法準確識別,導致一些需要明確限制圖片格式的功能,無法做到限制抛姑。
工具:使用 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();
}
使用了一些圖片定硝,測試了其格式判斷,基本上是沒問題