java 获取本地IP

import java.net.*;

/**
* This program demonstrates the InetAddressTest class. Supply a host name
* as command line argument, or run without command lines to see the address
* of the local host
*/

public class InetAddressTest {
    public static void main(String[] args) {
        try {
            if (args.length > 0) {
                String host = args[0];
                InetAddress[] addresses = InetAddress.getAllByName(host);
                for (InetAddress a  : addresses) {
                    System.out.println(a);
                }
            } else {
                InetAddress localHostAddress = InetAddress.getLocalHost();
                System.out.println(localHostAddress);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

标签: JAVA,IP
posted @ 2011-12-01 22:41  Mingxx  阅读(165)  评论(0)    收藏  举报