java 获取IP地址(要是安装了虚拟机之类的软件可能不准确)
2017-12-07 13:55
579 查看
/** * 多IP处理,可以得到最终ip * * @return */ public String getIP() { String localip = null;// 本地IP,如果没有配置外网IP则返回它 String netip = null;// 外网IP try { Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// 是否找到外网IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = 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) { logger.error("获取本机IP异常", e); } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } }
相关文章推荐
- CPC客户端离线升级失败,不能获取updatesipo信息,可能你的软件在线更新程序没有安装
- 获取操作系统信息(本机IP地址,Java版本,安装路径等)
- java获取安装软件列表(64位和32位)
- Java准确地获取本地IP地址
- Java获取系统安装软件列表
- java实现获取安卓设备里已安装的软件包
- JAVAWEB开发之——Linux命令实战、虚拟机和centos的安装以及联网、常用命令、vi编辑以及软件的安装、sftp的使用
- 如何在诺基亚NOKIA手机安装JAVA软件程序
- JAVA编程获取IP地址
- 实现获取主机名称,IP地址的类 (java)
- JAVA IP地址相关:用Java来获取访问者真实的IP地址,IP地址转化成long型数的算法
- 用Java来获取访问者真实的IP地址
- 安装软件出现Error 1327 Invalid Drive X:/(X为盘符,有可能是C~Z)的解决方法
- Java获取IP地址:request.getRemoteAddr()
- 用Java来获取访问者真实的IP地址
- Java实例教程(1)JDK的获取、安装与配置
- 用Java代码在wicket页面获取客户端IP地址
- 获取本机已安装的软件列表C#/VB.NET(Get the list of installed software in local pc)
- Java混乱器Zelix KlassMaster逆向工程(一)软件中字符串信息的获取
- Java获取客户端真实IP地址的两种方法