Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查找url是否引用现有对象_Java_Url - Fatal编程技术网

Java 查找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(在我的情况下是图像)。有人知道如果我的url引用的是未从存储中删除的图像,我该如何获取

像这样的东西怎么样

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")));
}

响应代码可能会有所帮助