zoukankan      html  css  js  c++  java
  • java如何获取IP地址

    import java.net.Inet6Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.Enumeration;
     
    public class GetMacIp {
     
           /**  
           * @Title: main 
           * @Description: TODO
           * @param :@param args     
           * @return :void    
           * @throws 
           */
           public static void main(String[] args) {
                
                        String currentIP= null;
                        Enumeration<?> e1;
                        try {
                            e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
                            while (e1.hasMoreElements()) {
                                NetworkInterface ni = (NetworkInterface) e1.nextElement();
                                if (!ni.getName().equals("eth3" )) {
                                    continue;
                                } else {
                                    Enumeration<?> e2 = ni.getInetAddresses();
                                    while (e2.hasMoreElements()) {
                                        InetAddress ia = (InetAddress) e2.nextElement();
                                        if (ia instanceof Inet6Address) {
                                            continue;
                                        }
                                       currentIP = ia.getHostAddress();
                                       
                                    }
                                    System. out.println("======" +currentIP);
                                    break;
                                }
                            }
                        } catch (SocketException e) {
                            e.getMessage();
                        }
     
                    }
     
          }
  • 相关阅读:
    Python 练习册,每天一个小程序----第0000题
    CMDB系统原创开发
    203.pycharm连接远程终端
    202. 菜鸟学习k8s安装1
    (高并发)防止重复点击,屏蔽多次无效请求的解决方案(优惠劵被重复领取,恶意撸羊毛)
    SSM批量插入和修改实现实例
    java服务器访问其他服务器工具类编写
    递归实例,欢迎大家修改优化
    SpringJDBC
    Spring入门
  • 原文地址:https://www.cnblogs.com/beantestng/p/3772848.html
Copyright © 2011-2022 走看看