private String getLocalSiteIP() throws Exception {
String siteString = "";
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
InetAddress ip = (InetAddress) ni.getInetAddresses().nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() &&
ip.getHostAddress().indexOf(":") == -1) {
System.out.println("Interface " + ni.getName() +
" seems to be InternetInterface. I'll take it...");
siteString = ip.getHostAddress();
}
}
return siteString;
}对单网卡有用,windows/linux都调通,希望对你有用,多网卡还是让用户配置吧
String siteString = "";
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
InetAddress ip = (InetAddress) ni.getInetAddresses().nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() &&
ip.getHostAddress().indexOf(":") == -1) {
System.out.println("Interface " + ni.getName() +
" seems to be InternetInterface. I'll take it...");
siteString = ip.getHostAddress();
}
}
return siteString;
}对单网卡有用,windows/linux都调通,希望对你有用,多网卡还是让用户配置吧
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货