java获得本机IP,名称等

import java.net.InetAddress;
import java.net.UnknownHostException;


public class GetLocalIP {
    public static void main(String[] args) {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            String ip=addr.getHostAddress().toString();//获得本机IP
            String address=addr.getHostName().toString();//获得本机名称
            System.out.println(ip+"--------------"+address);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2014-03-11 09:04  流星焱雨  阅读(325)  评论(0编辑  收藏  举报