子网掩码及IP获取

使用java获取子网掩码,用于判断是否为内网IP,代码如下:

NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()) ;
	List<InterfaceAddress> list = networkInterface.getInterfaceAddresses() ;
	for (InterfaceAddress interfaceAddress : list) {
		InetAddress inetAddress = interfaceAddress.getAddress() ;
		System.out.println("子网掩码位数:"+interfaceAddress.getNetworkPrefixLength()+";本机IP:"+inetAddress.getHostAddress());
	}

获取本机mac地址,参考页面:http://lpyyn.iteye.com/admin/blogs/2108263

你可能感兴趣的:(IP)