Java 查找url是否引用现有对象
我有很多URL在客户端上显示它。但我必须选择指向现有对象的URL(在我的情况下是图像)。有人知道如果我的url引用的是未从存储中删除的图像,我该如何获取 像这样的东西怎么样Java 查找url是否引用现有对象,java,url,Java,Url,我有很多URL在客户端上显示它。但我必须选择指向现有对象的URL(在我的情况下是图像)。有人知道如果我的url引用的是未从存储中删除的图像,我该如何获取 像这样的东西怎么样 URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); int st
URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
int statusCode = connection.getResponseCode();//OK if returned value is 200
您可以尝试打开一个流:
public static boolean exists(URL url) throws IOException {
try {
InputStream inputStream = url.openStream();
return inputStream != null;
}catch (FileNotFoundException e){
return false;
}
}
public static void main(String[] args) throws Exception {
System.out.println(exists(new URL("https://www.google.de/images/srpr/logo11w.png")));
System.out.println(exists(new URL("https://www.google.de/images/srpr/foo.png")));
}
响应代码可能会有所帮助