Java URL Exist exists(URL url)

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

Description

exists

License

Open Source License

Declaration

public static boolean exists(URL url) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

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

public class Main {
    public static boolean exists(String location) {
        try {/* w ww.j  a v a2s  . c om*/
            return exists(newURL(location));
        } catch (IOException e) {
            return false;
        }
    }

    public static boolean exists(URL url) {
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("HEAD");
            return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
        } catch (IOException e) {
            return false;
        }
    }

    public static URL newURL(String location) throws IOException {
        return newURI(location).toURL();
    }

    public static URI newURI(String location) throws IOException {
        try {
            return URI.create(location);
        } catch (IllegalArgumentException e) {
            throw new IOException("malformed uri: " + location, e);
        }
    }
}

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)
  7. existsURL_bak(String url)