>  기사  >  Java  >  Java는 이 시스템에 있는 모든 네트워크 카드의 IP 주소를 가져옵니다(ipv4).

Java는 이 시스템에 있는 모든 네트워크 카드의 IP 주소를 가져옵니다(ipv4).

巴扎黑
巴扎黑원래의
2016-12-28 17:55:052296검색

/**
* 获取机器所有网卡的IP(ipv4)
* @return
*/
public static List<String> getLocalIP() {
List<String> ipList = new ArrayList<String>();
InetAddress ip = null;
try {
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
// 遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (null == ip || "".equals(ip)) {
continue;
}
String sIP = ip.getHostAddress();
if(sIP == null || sIP.indexOf(":") > -1) {
continue;
}
ipList.add(sIP);
System.out.println(sIP);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return ipList;
}


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.