protected String getIp() { Enumeration<NetworkInterface> netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { InetAddress ip = ips.nextElement(); if (ip.isSiteLocalAddress()) { return ip.getHostAddress(); } } } } catch (Exception e) { e.printStackTrace(); } return ""; }
public static final String ip = null; public static final hostName = null; static { try { hostName = InetAddress.getLocalHost().getHostName(); InetAddress ipAddr[] = InetAddress.getAllByName(hostName); for (int i = 0; i < ipAddr.length; i++) { ip = ipAddr[i].getHostAddress(); if (ip.startsWith(LOCAL_IP_STAR_STR)) { break; } } if (ip == null) { ip = ipAddr[0].getHostAddress(); } } catch (UnknownHostException e) { } }