肿瘤康复网,内容丰富有趣,生活中的好帮手!
肿瘤康复网 > java获取网络ip地址_Java获取电脑真实IP地址的示例代码

java获取网络ip地址_Java获取电脑真实IP地址的示例代码

时间:2018-10-24 23:05:18

相关推荐

/**

* @author yins

* @date 8月12日下午9:53:58

*/

import .Inet4Address;

import .InetAddress;

import workInterface;

import .SocketException;

import java.util.Enumeration;

/**

* 获取本地真正的IP地址,即获得有线或者无线WiFi地址。

* 过滤虚拟机、蓝牙等地址

* @author yins

* @date 8月12日 下午9:53:58

*/

public class GetRealLocalIP {

/**

* 获取本地真正的IP地址,即获得有线或者无线WiFi地址。

* 过滤虚拟机、蓝牙等地址

* @author yins

* @date 8月12日下午9:56:35

* @return

*/

public static String getRealIP() {

try {

Enumeration allNetInterfaces = NetworkInterface

.getNetworkInterfaces();

while (allNetInterfaces.hasMoreElements()) {

NetworkInterface netInterface = (NetworkInterface) allNetInterfaces

.nextElement();

// 去除回环接口,子接口,未运行和接口

if (netInterface.isLoopback() || netInterface.isVirtual()

|| !netInterface.isUp()) {

continue;

}

if (!netInterface.getDisplayName().contains("Intel")

&& !netInterface.getDisplayName().contains("Realtek")) {

continue;

}

Enumeration addresses = netInterface

.getInetAddresses();

System.out.println(netInterface.getDisplayName());

while (addresses.hasMoreElements()) {

InetAddress ip = addresses.nextElement();

if (ip != null) {

// ipv4

if (ip instanceof Inet4Address) {

System.out.println("ipv4 = " + ip.getHostAddress());

return ip.getHostAddress();

}

}

}

break;

}

} catch (SocketException e) {

System.err.println("Error when getting host ip address"

+ e.getMessage());

}

return null;

}

}

此代码中只要读取到了WiFi或者有线地址其中之一立即return。

以上就是Java获取电脑真实IP地址的示例代码的详细内容,更多关于Java获取IP地址的资料请关注云海天教程其它相关文章!

原文链接:/lm970585581/p/13366139.html

如果觉得《java获取网络ip地址_Java获取电脑真实IP地址的示例代码》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。