
java获取外网ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
Systemoutprintln(MaingetRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
Enumeration<NetworkInterface> netInterfaces =
NetworkInterfacegetNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaceshasMoreElements() && !finded) {
NetworkInterface ni = netInterfacesnextElement();
Enumeration<InetAddress> address = nigetInetAddresses();
while (addresshasMoreElements()) {
ip = addressnextElement();
if (!ipisSiteLocalAddress()
&& !ipisLoopbackAddress()
&& ipgetHostAddress()indexOf(":") == -1) {// 外网IP
netip = ipgetHostAddress();
finded = true;
break;
} else if (ipisSiteLocalAddress()
&& !ipisLoopbackAddress()
&& ipgetHostAddress()indexOf(":") == -1) {// 内网IP
localip = ipgetHostAddress();
}
}
}
if (netip != null && !""equals(netip)) {
return netip;
} else {
return localip;
}
}
}
原来的>
如下图所示:
以上就是关于JAVA如何获得外网IP地址全部的内容,包括:JAVA如何获得外网IP地址、vb 不上ip 138 获取外网IP、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)