import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
/**
* @author benfreer
* @date 2019/12/27
*/
public class Test {
public static void main(String[] args) {
try {
File f = new File("d:/00541e5e-2286-409f-8871-1ccc3edb243f.jpg");
FileInputStream fi = new FileInputStream(f);
try {
BufferedImage sourceImg = ImageIO.read(fi);//判断图片是否损坏
int picWidth = sourceImg.getWidth(); //确保图片是正确的(正确的图片可以取得宽度)
} catch (Exception e) {
// TODO: handle exception
fi.close();//关闭IO流才能操作图片
return;
} finally {
fi.close();//最后一定要关闭IO流
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.toString());
return;
}
}
}
当图片损坏时,sourceImg为空