Java URL Exist exists(String url)

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

Description

exists

License

Apache License

Declaration

public static boolean exists(String url) 

Method Source Code


//package com.java2s;
/*//from w  ww .j a  va  2 s .c  om
 * Copyright (C) 2008 Marco Aur?lio Graciotto Silva <magsilva@ironiacorp.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    public static boolean exists(String url) {
        try {
            return exists(new URL(url));
        } catch (Exception e) {
            return false;
        }
    }

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

Related

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