集册 Java实例教程 从Url获取域

从Url获取域

欢马劈雪     最近更新时间:2020-01-02 10:19:05

490
从网址获取域名
/**
nowjava.com - 时代Java
**/


//package com.nowjava;

import java.net.MalformedURLException;

import java.net.URISyntaxException;

import java.net.URL;


public class Main {

    public static String getDomainFromUrl(String url) {

        if (url == null || !isValidUrl(url))

            return "";

        return url.split("//")[1].split("/")[0];

    }


    public static boolean isValidUrl(String url) {

        if (url == null || url.length() == 0)

            return false;

            /* 
            *来 自
             时代Java
            */

        /*

         * from stackoverflow:

         * http://stackoverflow.com/questions/1600291/validating-url-in-java

         */

        URL u = null;


        try {

            u = new URL(url);

        } catch (MalformedURLException e) {

            return false;

 
展开阅读全文