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();
                        }
     
                    }
     
          }
  • 相关阅读:
    挂载磁盘不成功显示mount: /mnt: wrong fs type, bad option, bad superblock..............
    Linux如何查看文件的创建、修改时间?
    Linux,Centos下 Tomcat8 修改jvm内存配置的新方法
    ActiveMQ问题分析和解决
    centos如何删除文件夹
    CentOS 几种重启方式的区别
    CentOS查看文件夹大小
    挂在光盘出现写保护mount: block device /dev/sr0 is writeprotected, mounting readonly
    centos7安装activeMq
    Rust中的workspace
  • 原文地址:https://www.cnblogs.com/beantestng/p/3772848.html
Copyright © 2011-2022 走看看