Here you can find the source of URLExists(URL url, StringBuffer errorMsg)
Parameter | Description |
---|---|
url | the URL in question. |
errorMsg | a string buffer in which to print any error message. If null, errors are not printed. |
public static boolean URLExists(URL url, StringBuffer errorMsg)
//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; } }