java查找url是否引用现有对象 1 月 Questions & Answers 50 我有很多URL可以在客户端上显示。但我必须选择指向现有对象的URL(在我的情况下是图像)。有人知道如果我的url引用的是未从存储中删除的图像,我该如何获取
# 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
# 2 楼答案 您可以尝试打开一条流: 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"))); }
# 1 楼答案
像这样的怎么样
# 2 楼答案
您可以尝试打开一条流: