判定图片是否能正常打开

xiaoxiao2021-02-28  4

实际项目运用中常常会涉及到图片,图片会遇到打不开的情况。涉及到的原因也有很多:

1.第三方服务拽起图片失败

2.自身的服务由于性能瓶颈的原因下载到本地失败

3.本身的图片就有问题,比如大小就是0kb

我们要知道次图片是否是正常,好做我们实际的业务情况!

总而言之,后台如何判定下载下来的图片没有问题,是完整的呢?

后台代码如下:

/** * 判定是否是正常的图片文件 * @param file * @return * @throws IOException */ public static boolean isPictureFile(File file) throws IOException { if(file.exists()) { Image image= ImageIO.read(file); if (image == null) { return false; } return true; } else { return false; } } 利用Image的特性,若是图片则会返回image的实力,若是非图片信息,则会返回为null!

转载请注明原文地址: https://www.6miu.com/read-1150153.html

最新回复(0)