linux/Windows中JAVA获取IP地址

来源:互联网 发布:语言障碍评估软件 编辑:程序博客网 时间:2024/04/24 03:31
public  void getIpAddr() {Enumeration allNetInterfaces = null;try {allNetInterfaces = NetworkInterface.getNetworkInterfaces();} catch (SocketException e) {e.printStackTrace();}InetAddress ip = null;while (allNetInterfaces.hasMoreElements()) {NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();System.out.println(netInterface.getName());Enumeration addresses = netInterface.getInetAddresses();while (addresses.hasMoreElements()) {ip = (InetAddress) addresses.nextElement();if (ip != null && ip instanceof Inet4Address&& ip.getHostAddress().indexOf(".") != -1) {System.out.println("本机的IP = " + ip.getHostAddress());logger.debug("------------------本机的IP : "+ip.getHostAddress()+"---------------------");}}}}

 
原创粉丝点击