Java中InetAddress的基本用法, 可用于判断地址为IPv4 or IPv6
InetAddress address = InetAddress.getByName(host); System.out.println("IP: " + address.getHostAddress()); int length = address.getAddress().length; System.out.println("IP Address length: " + length); //根据byte数组长度判断这个IP地址是IPv4/IPv6地址: length为4表示IPv4, 为16表示Ipv6 //使用instanceof判断这个IP地址是IPv4/IPv6地址 if (address instanceof Inet4Address) { System.out.println("This is IPv4 address according to instanceof !"); } else if (address instanceof Inet6Address) { System.out.println("This is IPv6 address according to instanceof !"); }
package com.hulk.http.util;
import com.mobile.emm.debugger.Log;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
/**
* InetAddress单元测试
* @author: zhanghao
* @Time: 2021-06-19 16:43
*/
public class InetAddressTest {
private static final String TAG = "InetAddressTest";
public static void main(String[] args) {
System.out.println("InetAddressTest.main: args= " + Arrays.toString(args));
try {
//测试v4域名
test("www.baidu.com");
//"www.baidu.com"
test("220.181.38.150");
test("www.qianxin.com");
//IPV6测试
test("www.neu6.edu.cn");
test("2001:da8:9000:e013:0:0:199:4");;
//测试内网地址,如果域名地址无法解析,
test("10.195.155.217");
test("www.hahaha.hulk.com");
} catch (Exception e) {
System.err.println("test failed: " + e);
}
}
public static void test(String host) {
System.out.println("\n>>>>test host: " + host);
InetAddress address = null;
try {
address = InetAddress.getByName(host);
} catch (Exception e) {
Log.e(TAG, "test UnknownHostException: " + e);
}
if (address == null) {
System.err.println("test: Failed, address is null, for host " + host);
return;
}
System.out.println("IP: " + address.getHostAddress());
//根据byte数组长度判断这个IP地址是IPv4/IPv6地址!
int length = address.getAddress().length;
System.out.println("IP Address length: " + length);
switch (length) {
case 4:
System.out.println("This is IPv4 address according to byte array length !");
break;
case 16:
System.out.println("This is IPv6 address according to byte array length !");
break;
default:
System.out.println("Unknown address length " + length);
break;
}
//使用instanceof判断这个IP地址是IPv4/IPv6地址
if (address instanceof Inet4Address) {
System.out.println("This is IPv4 address according to instanceof !");
} else if (address instanceof Inet6Address) {
System.out.println("This is IPv6 address according to instanceof !");
}
}
}
以上代码运行结果如下:
com.mobile.emm.http.util.InetAddressTest
InetAddressTest.main: args= []
>>>>test net host: www.baidu.com
IP: 220.181.38.150
IP Address length: 4
This is IPv4 address according to byte array length !
This is IPv4 address according to instanceof !
>>>>test net host: 220.181.38.150
IP: 220.181.38.150
IP Address length: 4
This is IPv4 address according to byte array length !
This is IPv4 address according to instanceof !
>>>>test net host: www.qianxin.com
IP: 61.160.224.57
IP Address length: 4
This is IPv4 address according to byte array length !
This is IPv4 address according to instanceof !
>>>>test net host: www.neu6.edu.cn
IP: 2001:da8:9000:e013:0:0:199:4
IP Address length: 16
This is IPv6 address according to byte array length !
This is IPv6 address according to instanceof !
>>>>test net host: 2001:da8:9000:e013:0:0:199:4
IP: 2001:da8:9000:e013:0:0:199:4
IP Address length: 16
This is IPv6 address according to byte array length !
This is IPv6 address according to instanceof !
>>>>test net host: 10.195.155.217
IP: 10.195.155.217
IP Address length: 4
This is IPv4 address according to byte array length !
This is IPv4 address according to instanceof !
>>>>test net host: www.hahaha.hulk.com //地址无法解析会抛出异常
test failed: java.net.UnknownHostException: www.hahaha.hulk.com: Name or service not known
Process finished with exit code 0