Java URL Connection URLExists(URL url, StringBuffer errorMsg)

Here you can find the source of URLExists(URL url, StringBuffer errorMsg)

Description

Method to tell whether a given URL exists.

License

Open Source License

Parameter

Parameter Description
url the URL in question.
errorMsg a string buffer in which to print any error message. If null, errors are not printed.

Return

true if the file exists.

Declaration

public static boolean URLExists(URL url, StringBuffer errorMsg) 

Method Source Code

//package com.java2s;
/* -*- tab-width: 4 -*-//from  www .jav  a2s . co m
 *
 * Electric(tm) VLSI Design System
 *
 * File: TextUtils.java
 *
 * Copyright (c) 2003 Sun Microsystems and Static Free Software
 *
 * Electric(tm) 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 3 of the License, or
 * (at your option) any later version.
 *
 * Electric(tm) 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 Electric(tm); see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, Mass 02111-1307, USA.
 */

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

public class Main {
    /**
     * Method to tell whether a given URL exists.
     * @param url the URL in question.
     * @return true if the file exists.
     */
    public static boolean URLExists(URL url) {
        return URLExists(url, null);
    }

    /**
     * Method to tell whether a given URL exists.
     * @param url the URL in question.
     * @param errorMsg a string buffer in which to print any error message.
     * If null, errors are not printed.
     * @return true if the file exists.
     */
    public static boolean URLExists(URL url, StringBuffer errorMsg) {
        if (url == null) {
            return false;
        }

        try {
            URLConnection con = url.openConnection();
            con.connect();
            int conLength = con.getContentLength();
            con.getInputStream().close();
            if (conLength < 0) {
                return false;
            }
        } catch (Exception e) {
            if (errorMsg != null) {
                errorMsg.append("Error: cannot open " + e.getMessage() + "\n");
            }
            return false;
        }
        return true;
    }
}

Related

  1. slurpURL(URL u, String encoding)
  2. slurpURLNoExceptions(URL u)
  3. url2list(String sURL, String encoding)
  4. urlContents(String urlString)
  5. urlExists(String urlString)
  6. urlifyMap(Long nid, double latitude, double longitude)
  7. urlToHtm(String word, String findurl, String path)
  8. urlToReader(URL url)
  9. urlToString(String url)