集册 Java实例教程 如果IP地址为IPv4地址,则将IP地址字符串解析为字节数组;如果IP地址为IPv6地址,则字节数组长度为4;如果主机不是IP地址字符串,则字节数组长度为16,将返回空值;

如果IP地址为IPv4地址,则将IP地址字符串解析为字节数组;如果IP地址为IPv6地址,则字节数组长度为4;如果主机不是IP地址字符串,则字节数组长度为16,将返回空值;

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

697
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
如果IP地址为IPv4地址,则将IP地址字符串解析为字节数组;如果IP地址为IPv6地址,则字节数组长度为4;如果主机不是IP地址字符串,则字节数组长度为16,将返回空值;
// 来 自 N  o w  J a v a . c o m

//package com.nowjava;

import sun.net.util.IPAddressUtil;


public class Main {

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

        String ipAddress = "nowjava.com";

        System.out.println(java.util.Arrays

                .toString(parseIpAddress(ipAddress)));

    }


    /**

     * This function parse the IP address string as a byte array

     * If the IP address is IPv4 address, the byte array length is 4

     * If the IP address is IPv6 address, the byte array length is 16

     * If the host is not an IP address string, will return null;

     * @param ipAddress

     * @return if this is valid IPv4 address or IPv6 address, return byte array for the address

     * otherwise return null.

     * So, this method can be used to check if the ipAddress string is valid or not

     */

    public static byte[] parseIpAddress(String ipAddress) {

        if (ipAddress == null)

            return null;

        boolean ipv6Expected = false;

        if (ipAddress.charAt(0) == '[') {
        /**
        时代Java - N o w  J a v a . c o m
        **/

            // This is supposed to be an IPv6 literal

            if (ipAddress.length() > 2

                    && ipAddress.charAt(ipAddress.length() - 1) == ']') {

                ipAddress = ipAddress.substring(1, ipAddress.length() - 1);

                ipv6Expected = true;

            } else {

                // This was supposed to be a IPv6 address, but it's not!

                //throw new UnknownHostException(host + ": invalid IPv6 address");

                return null;

            }

        }


        // if host is an IP address, we won't do further lookup

        if (Character.digit(ipAddress.charAt(0), 16) != -1

                || (ipAddress.charAt(0) == ':')) {

            byte[] addressBytes = null;

            //            int numericZone = -1;

            //            String ifname = null;


            // see if it is IPv4 address

            addressBytes = IPAddressUtil.textToNumericFormatV4(ipAddress);

            if (addressBytes == null) {

                // see if it is IPv6 address

                // Check if a numeric or string zone id is present

                //                int pos;

                //                if ((pos=ipAddress.indexOf ("%")) != -1) {

                //                    numericZone = checkNumericZone (ipAddress);

                //                    if (numericZone == -1) { /* remainder of string must be an ifname */

                ////                        ifname = ipAddress.substring (pos+1);

                //                    }

                //                }

                addressBytes = IPAddressUtil

                        .textToNumericFormatV6(ipAddress);

            } else 
展开阅读全文