码迷,mamicode.com
首页 > 编程语言 > 详细

Java获取ip地址的几种方法

时间:2014-11-01 13:27:12      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   ar   java   sp   文件   

以下内容介绍下java获取ip地址的几种思路。

1、直接利用java.net.InetAddress类获取,不过这种方法只在windows环境下有效,在linux环境下只能获取localhost地址(即/etc/hosts文件内容)

  代码如下: 

 1     import java.net.InetAddress;
 2 
 3     /**
 4      * This method works well in windows system.
 5      * In Linux system it returns 127.0.0.1 the content of the hosts file.
 6      */
 7     public static void getIpAddressInWindows() {
 8         try {
 9             InetAddress address = InetAddress.getLocalHost();
10             System.out.println("Host Name: " + address.getHostName());
11             System.out.println("Host Address: " + address.getHostAddress());
12 
13         } catch (UnknownHostException e) {
14             e.printStackTrace();
15         }
16     } 

2、可以在linux下正常工作的方法,代码如下:

    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Enumeration;

    /**
     * This method is used to get all ip addresses from the network interfaces.
     * network interfaces: eth0, wlan0, l0, vmnet1, vmnet8 
     */
    public static void getAllIpAddress() {
        try {
            //get all network interface
            Enumeration<NetworkInterface> allNetworkInterfaces = 
                    NetworkInterface.getNetworkInterfaces();
            NetworkInterface networkInterface = null;
            
            //check if there are more than one network interface
            while (allNetworkInterfaces.hasMoreElements()) {
                //get next network interface
                networkInterface = allNetworkInterfaces.nextElement();
                //output interface‘s name
                System.out.println("network interface: " + 
                        networkInterface.getDisplayName());
                
                                //get all ip address that bound to this network interface
                Enumeration<InetAddress> allInetAddress = 
                        networkInterface.getInetAddresses();
                
                InetAddress ipAddress = null;
                
                //check if there are more than one ip addresses
                //band to one network interface
                while (allInetAddress.hasMoreElements()) {
                    //get next ip address
                    ipAddress = allInetAddress.nextElement();
                    if (ipAddress != null && ipAddress instanceof Inet4Address) {
                        System.out.println("ip address: " + 
                                ipAddress.getHostAddress());
                    }
                }
            }
            
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }//end method getAllIpAddress
                            

上边这种方法有些不足之处,它会输出所有的网卡上的ip地址,有时候我们只需一个或几个单独的网卡ip即可,可以通过如下方法获得:    

 1

      import java.net.Inet4Address;
      import java.net.InetAddress;
      import java.net.NetworkInterface;
      import java.net.SocketException;
      import java.net.UnknownHostException;
      import java.util.Enumeration;

       /**
 2      * This method is used to get ip address by network interface‘s name.
 3      * @param networkInterfaceName network interface‘s name
 4      * @return return true if get ip address successfully,
 5      * otherwise return false. 
 6      */
 7     public static boolean getIpAddrByName(String networkInterfaceName) {
 8         try {
 9         //get network interface by name
10             NetworkInterface networkInterface = 
11                     NetworkInterface.getByName(networkInterfaceName);
12             if (networkInterface == null) {
13                 return false;
14             }
15             System.out.println("network interface: " + 
16                     networkInterface.getDisplayName());
17             
18             InetAddress ipAddress = null;
19             //get all ip addresses band to this interface
20             Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
21             
22             while (addresses.hasMoreElements()) {
23                 ipAddress = addresses.nextElement();
24                 
25                 if (ipAddress != null && ipAddress instanceof Inet4Address) {
26                     System.out.println("ip address: " + 
27                             ipAddress.getHostAddress());
28                 }
29             }
30         } catch (SocketException e) {
31             e.printStackTrace();
32         }
33         
34         return true;
35     }// end method getIpAddrByName

Java获取ip地址的几种方法

标签:style   blog   io   color   os   ar   java   sp   文件   

原文地址:http://www.cnblogs.com/blog-zuo/p/4067018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!