集册 Java实例教程 ip到号码

ip到号码

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

484
ip到号码
/*nowjava - 时代Java 提 供*/


import java.util.ArrayList;

import java.util.regex.Pattern;


public class Main{

    public static void main(String[] argv) throws Exception{

        String ipAddr = "nowjava.com";

        System.out.println(ipToNumber(ipAddr));

    }

    public static long ipToNumber(String ipAddr)

            throws InvalidIPAddressException {

        if (ipAddr == null) {

            throw new InvalidIPAddressException("NULL IP Address");
            /* 
             来自 
            *时 代 J     a    v  a - nowjava.com*/

        }


        String[] addrArray = ipAddr.split("\\.");


        if (addrArray.length != 4) {

            throw new InvalidIPAddressException("Invalid IP Address length");

        }


        long num = 0;

        for (int i = 0; i < addrArray.length; i++) {

            int power = 3 - i;


            int octet = Integer.parseInt(addrArray[i]);


            if (octet < 0 || octet > 255) {

                
展开阅读全文