2014-08-28 32 views
0

我有很多網址在客戶端顯示。但我必須選擇對現有對象的URL(在我的情況 - 圖像)。有人知道如果我的網址指的是沒有從存儲中刪除的圖像,我怎麼能得到?查找url是否指向現有對象

+0

響應代碼可能會有所幫助 – SparkOn 2014-08-28 13:25:30

回答

1

如何像

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 
1

你可以嘗試打開一個流:

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"))); 
} 
相關問題