JAVA获取访问者的内网IP地址
/** * 获取访问者内网IP * @return the server ip */ public static String getIntranetIp() { // 本地IP,如果没有配置外网IP则返回它 String localip = null; // 外网IP String netip = null; try { Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; // 是否找到外网IP boolean finded = false; while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); Enumeration address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = (InetAddress) address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) { // 外网IP netip = ip.getHostAddress(); finded = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) { // 内网IP localip = ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)