首页 > 编程语言 > 详细

Java 获取本机IP

时间:2015-01-27 01:45:20      阅读:287      评论:0      收藏:0      [点我收藏+]
import java.io.*;
import java.util.*;
import java.net.*;

public class GetIP {
    public static void main (String[] args) throws Exception {
        /*
        String s = InetAddress.getLocalHost().toString();
        System.out.println(s);
        
        String[] arr = s.split("/");
        System.out.println("host name:\n" + arr[0]);
        System.out.println("localhost IP:\n" + arr[1]);
        
        */
        Enumeration e = NetworkInterface.getNetworkInterfaces();
        while(e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                System.out.println(i.getHostAddress());
            }
        }

    }
    
    
}

技术分享

Linux上也适用。

 

Java 获取本机IP

原文:http://www.cnblogs.com/duanguyuan/p/4251728.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!