Java URL Value Check isLegalURL(final String url)

Here you can find the source of isLegalURL(final String url)

Description

Proves if the given string is a valid URL.

License

Open Source License

Parameter

Parameter Description
url An URL as String

Return

true if the string is a valid URL, otherwise false.

Declaration

public static boolean isLegalURL(final String url) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w  w  w. j a v  a 2  s  .  c  o m
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * Proves if the given string is a valid URL. This method tries to open the
     * URL to check its validity.
     * 
     * @param url
     *            An URL as String
     * @return <tt>true</tt> if the string is a valid URL, otherwise
     *         <tt>false</tt>.
     */
    public static boolean isLegalURL(final String url) {
        if ((url == null) || (url.trim().length() == 0)) {
            return false;
        }

        try {
            final URL u = new URL(url);
            u.openConnection();
            return true;
        } catch (final MalformedURLException e) {
            return false;
        } catch (final IOException e) {
            return false;
        }
    }
}

Related

  1. isJarURL(URL url)
  2. isJarURL(URL url)
  3. isJarURL(URL url)
  4. isJarURL(URL url)
  5. isJBoss5Url(URL fileUrl)
  6. isLocal(URL aURL)
  7. isLocalFile(URL url)
  8. isLocalFile(URL url)
  9. isLocalFile(URL url)