论坛首页 Java企业应用论坛

Java 获取本机IP

浏览 7787 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-27   最后修改:2010-11-09

开始我在Linux下用InetAddress.getLocalHost()方法获取本机IP地址,
得到的结果总是:127.0.1.1。
原来这个是etc/hosts文件中的配置,并非网卡的IP地址。
后来多方寻访,终于得下以下代码,
运行后在控制台输出IP与


开始我在Linux下用InetAddress.getLocalHost()方法获取本机IP地址,
得到的结果总是:127.0.1.1。
原来这个是etc/hosts文件中的配置,并非网卡的IP地址。
后来多方寻访,终于得下以下代码,
运行后在控制台输出IP与MAC地址。
import java.net.*;
import java.util.*;
public class getIP {
   public static void main(String[] args) {
   getIP t = new getIP();
   System.out.println(t.getLocalIP());
   System.out.println(t.getMacAddr());
   }
   public String getMacAddr() {  
       String MacAddr = "";
       String str = "";
       try {
           NetworkInterface NIC = NetworkInterface.getByName("eth0");
           byte[] buf = NIC.getHardwareAddress();
           for (int i = 0; i < buf.length; i++) {
               str = str + byteHEX(buf[i]);
           }
           MacAddr = str.toUpperCase();
       } catch (SocketException e) {
           e.printStackTrace();
           System.exit(-1);
       }
       return MacAddr;
   }
   public String getLocalIP() {
       String ip = "";
       try {
           Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
           while (e1.hasMoreElements()) {
               NetworkInterface ni = (NetworkInterface) e1.nextElement();
               if (!ni.getName().equals("eth0")) {
                   continue;
               } else {
                   Enumeration<?> e2 = ni.getInetAddresses();
                   while (e2.hasMoreElements()) {
                       InetAddress ia = (InetAddress) e2.nextElement();
                       if (ia instanceof Inet6Address)
                           continue;
                       ip = ia.getHostAddress();
                   }
                   break;
               }
           }
       } catch (SocketException e) {
           e.printStackTrace();
           System.exit(-1);
       }
       return ip;
   }
/* 一个将字节转化为十六进制ASSIC码的函数 */
   public static String byteHEX(byte ib) {
       char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a','b', 'c', 'd', 'e', 'f' };
       char[] ob = new char[2];
       ob[0] = Digit[(ib >>> 4) & 0X0F];
       ob[1] = Digit[ib & 0X0F];
       String s = new String(ob);
       return s;
   }
}

在Windows下获得硬件地址,windows 7 测试可用:

import java.net.*;
import java.util.*;

public class GetIP
{
    public static void main (String[] args)
    {
        GetIP t = new GetIP ();
        //System.out.println (t.getLocalIP ());
        System.out.println (t.getMacAddr ());
    }

    public String getMacAddr ()
    {
        String MacAddr = "";
        String str = "";
        try
        {
            Enumeration <NetworkInterface> em = NetworkInterface.getNetworkInterfaces ();
            while (em.hasMoreElements ())
            {
                NetworkInterface nic = em.nextElement ();
                //System.out.println ("nic.getDisplayName ():" + nic.getDisplayName ());
                //System.out.println ("nic.getName ():" + nic.getName ());
                byte[] b = nic.getHardwareAddress ();
                if (b == null)
                {
                    continue;
                }
                for (int i = 0; i < b.length; i++)
                {
                    System.out.print (byteHEX(b[i])+"-");
                }
                System.out.println ();
            }
        }
        catch (SocketException e)
        {
            e.printStackTrace ();
            System.exit (-1);
        }
        return MacAddr;
    }

    public  String getLocalIP ()
    {
        String ip = "";
        try
        {
            Enumeration <?> e1 = (Enumeration <?>) NetworkInterface.getNetworkInterfaces ();
            while (e1.hasMoreElements ())
            {
                NetworkInterface ni = (NetworkInterface) e1.nextElement ();
                Enumeration <?> e2 = ni.getInetAddresses ();
                while (e2.hasMoreElements ())
                {
                    InetAddress ia = (InetAddress) e2.nextElement ();
                    ip = ia.getHostAddress ();
                }
            }
        }
        catch (SocketException e)
        {
            e.printStackTrace ();
            System.exit (-1);
        }
        return ip;
    }

    /* 一个将字节转化为十六进制ASSIC码的函数 */
    public static String byteHEX (byte ib)
    {
        char[] Digit =
        { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] ob = new char[2];
        ob[0] = Digit[(ib >>> 4) & 0X0F];
        ob[1] = Digit[ib & 0X0F];
        String s = new String (ob);
        return s;
    }
}
 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics