Java URL Value Check isLocalhost(URL url)

Here you can find the source of isLocalhost(URL url)

Description

Checks whether the given URL is a localhost address

License

Apache License

Declaration

public static boolean isLocalhost(URL url) 

Method Source Code

//package com.java2s;
/*//from w w  w  .jav  a2  s.  c  o m
 * JBoss, Home of Professional Open Source
 * Copyright 2012, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;

public class Main {
    /**
     * Checks whether the given {@link URL} is a localhost address
     */
    public static boolean isLocalhost(URL url) {
        try {
            // Check if the address is a valid special local or loop back
            InetAddress address = InetAddress.getByName(url.getHost());
            if (address.isAnyLocalAddress() || address.isLoopbackAddress()) {
                return true;
            }

            // Check if the address is defined on any interface
            try {
                return NetworkInterface.getByInetAddress(address) != null;
            } catch (SocketException e) {
                return false;
            }
        } catch (UnknownHostException e) {
            return false;
        }
    }
}

Related

  1. isLocalFile(URL url)
  2. isLocalFile(URL url)
  3. isLocalFile(URL url)
  4. isLocalFile(URL url)
  5. isLocalFile(URL url)
  6. isLocalURL(String s)
  7. isLocalURL(URL url)
  8. isLocalURL(URL url)
  9. isNewer(URL file, URL reference)