网上看到的取IP的方法,觉的挺好。还想再linux、window下都管用
private static 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;
}