http://nichehire.com/Nichehire/upload/img_job_photo_burhan393@gmail.com.jpg
我需要通过给定的 URL 知道图像是否存在。为此,我正在使用以下代码。在下面的代码中,如果没有发生错误并且 URL 不包含图像,我将返回 true,则将返回 false。但是上面给出的 URL 不包含图像,即使它返回 true。我的代码有问题吗?
public static boolean exists(String URLName) {
boolean result = false;
try {
InputStream input = (new URL(URLName)).openStream();
result = true;
} catch (IOException ex) {
System.out.println("Image doesnot exits :");
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
..图像是否存在..
ImageIO.read(URL)
Run Code Online (Sandbox Code Playgroud)
这真的是唯一的保证方式:
some.gif.[已解决] 这个源代码工作正常!
String url1 = "https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-xft1/t39.1997-6/p200x200/851575_126362190881911_254357215_n.png";
Image image = ImageIO.read(new URL(url1));
if(image != null){
System.out.println("IMAGE");
}else{
System.out.println("NOT IMAGE");
}
Run Code Online (Sandbox Code Playgroud)