http://nichehire.com/Nichehire/upload/img_job_photo_burhan393@gmail.com.jpg
我需要通过给定的 URL 知道图像是否存在。 为此,我使用下面的代码。在下面的代码中,如果没有错误发生,我将返回 true,如果 URL 不包含图像,则将返回 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;
}
最佳答案
..whether an image exists or not..
ImageIO.read(URL)
这确实是唯一方法来确保:
- URL 指向某物(不返回 HTTP 404)
- 服务器允许访问资源(例如不返回 HTTP 500)
- URL 末尾的数据实际上表示图像(如 Java 所理解的那样),而不是重命名为
some.gif
的文本文件。
关于java - Java如何通过读取URL判断图片是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15293339/