Here you can find the source of isValidURL(String url)
public static boolean isValidURL(String url)
//package com.java2s; //License from project: Open Source License import java.net.MalformedURLException; public class Main { public static boolean isValidURL(String url) { if (url.length() < 10) return false; url = url.trim();// w w w . j ava 2s . com if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ftp://")) { String host = null; try { host = getHostName(url); } catch (MalformedURLException e) { return false; } if (!isValidHostString(host)) return false; if (host.contains("..")) return false; } else {//unknown schema return false; } //it seems right return true; } public static String getHostName(String url) throws MalformedURLException { if (url == null || url.length() == 0) return ""; url = url.toLowerCase(); if (!url.startsWith("http://") && !url.startsWith("https://") && !url.startsWith("ftp://")) throw new MalformedURLException("Unknown schema!"); int doubleslash = url.indexOf("//"); doubleslash += 2; int end = url.length(); int idx = url.indexOf('/', doubleslash); if (idx > 0 && idx < end) end = idx; idx = url.indexOf(':', doubleslash); if (idx > 0 && idx < end) end = idx; idx = url.indexOf('&', doubleslash); if (idx > 0 && idx < end) end = idx; idx = url.indexOf('=', doubleslash); if (idx > 0 && idx < end) end = idx; idx = url.indexOf('#', doubleslash); if (idx > 0 && idx < end) end = idx; idx = url.indexOf('?', doubleslash); if (idx > 0 && idx < end) end = idx; return url.substring(doubleslash, end); } static public final boolean isValidHostString(String str) { str = str.trim(); for (int i = 0; i < str.length(); i++) if (!isValidHostChar(str.charAt(i))) return false; if (str.startsWith("-") || str.endsWith("-")) return false; if (str.startsWith(".") || str.endsWith(".")) return false; if (str.startsWith(":") || str.endsWith(":")) return false; int colon_count = 0; for (int i = 0; i < str.length(); i++) if (str.charAt(i) == ':') colon_count++; if (colon_count > 1) return false; if (str.endsWith("_")) return false; return true; } static private final boolean isValidHostChar(char ch) { return isAsciLetter(ch) || isAsciDigit(ch) || ch == '-' || ch == '.' || ch == '_' || ch == ':'; } static private final boolean isAsciLetter(char ch) { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); } static private final boolean isAsciDigit(char ch) { return (ch >= '0' && ch <= '9'); } }