Java中判断文件是否是图片文件
使用Image读取文件时,如果是非图像文件,则会返回null。
参考代码:
try {
Image=ImageIO.read(new File(name));
if (image == null) {
valid = false;
System.out.println("The file"+name+"could not be opened , it is not an image");
}
} catch(IOException ex) {
valid=false;
System.out.println("The file"+name+"could not be opened , an error occurred.");
}