Java URL Exist exists(URL url)

Here you can find the source of exists(URL url)

Description

exists

License

LGPL

Declaration

public static boolean exists(URL url) throws Exception 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.File;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static boolean exists(URL url) throws Exception {
        if ("http".equals(url.getProtocol())) {
            HttpURLConnection cnx = (HttpURLConnection) url.openConnection();
            return (HttpURLConnection.HTTP_OK == cnx.getResponseCode());
        } else if ("file".equals(url.getProtocol())) {
            File f = new File(url.getPath());
            return f.exists();
        }//ww w.  jav a2 s .com
        throw new UnsupportedOperationException("only 'http' and 'file' protocol supported :" + url.getProtocol());
    }
}

Related

  1. exists(String fileorUrl)
  2. exists(String url)
  3. exists(URL url)
  4. exists(URL url)
  5. existsHttp(String URLName)
  6. existsURL(String url)