• 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();
                        }
     
                    }
     
          }
  • 相关阅读:
    20200929-git地址
    20200917-1 每周例行报告
    20200917-2 词频统计
    20200917-3 白名单
    20200910-1 每周例行报告
    20200910-2 博客作业
    20200924-3 单元测试,结对
    20200924-2 功能测试
    20200924-5 四则运算试题生成,结对
    20200924-4 代码规范,结对要求
  • 原文地址:https://www.cnblogs.com/beantestng/p/3772848.html
Copyright © 2020-2023  润新知