Java URL Exist exists(String fileorUrl)

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

Description

exists

License

Open Source License

Declaration

public static boolean exists(String fileorUrl) 

Method Source Code

//package com.java2s;
/*//from w ww .  j av  a  2  s .c  o m
*   Copyright (C) 2011 Life Technologies Inc.
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 2 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;

import java.net.HttpURLConnection;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static boolean exists(String fileorUrl) {
        // p("Checking if " + fileorUrl + " exists");
        if (isUrl(fileorUrl)) {
            if (fileorUrl.startsWith("http")) {
                return existsHttp(fileorUrl);
            } else {
                try {
                    URL url = new URL(fileorUrl);
                    //  p(url + " looks ok");
                    try {
                        URLConnection uc = url.openConnection();
                        uc.setDoInput(true);
                        ;
                        uc.connect();
                        //p("Exists: url " + url );
                        return true;
                    } catch (Exception ex) {
                        p("Could not open url " + url + ":" + ex.getMessage());
                    }
                    return false;
                } catch (Exception ex) {
                    p(fileorUrl + " is not a valid  url");
                    return false;
                }
            }
        } else {
            File f = new File(fileorUrl);
            return f.exists();
        }
    }

    public static boolean isUrl(String path) {
        if (path == null || path.length() < 1) {
            return false;
        }
        int pos = path.indexOf("://");
        if (pos > 0 && pos < 10) {
            return true;
        } else {
            return false;
        }
    }

    static boolean existsHttp(String URLName) {
        try {
            HttpURLConnection.setFollowRedirects(false);

            // note : you may also need
            //        HttpURLConnection.setInstanceFollowRedirects(false)
            HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
            con.setRequestMethod("HEAD");
            con.setConnectTimeout(10000);
            int code = con.getResponseCode();

            boolean ex = (code == HttpURLConnection.HTTP_OK);
            p("Exists url " + URLName + ", code: " + code + "= " + ex);
            return ex;
        } catch (Exception e) {
            p("URL does not exist: " + e.getMessage());
            return false;
        }
    }

    private static void p(String string) {
        System.out.println("FileUtils:" + string);
    }
}

Related

  1. exists(String url)
  2. exists(URL url)
  3. exists(URL url)
  4. exists(URL url)