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

Java 获取本机IP

时间:2015-01-27 01:48:29      阅读:167      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!