JAVA獲取本地IP地址

Enumeration<NetworkInterface> netInterfaces = null;  

try {  
    netInterfaces = NetworkInterface.getNetworkInterfaces();  
    while (netInterfaces.hasMoreElements()) {  
        NetworkInterface ni = netInterfaces.nextElement();  
        System.out.println("DisplayName:" + ni.getDisplayName());  
        System.out.println("Name:" + ni.getName());  
        Enumeration<InetAddress> ips = ni.getInetAddresses();  
        while (ips.hasMoreElements()) {  
            System.out.println("IP:" 
            + ips.nextElement().getHostAddress());  
        }  
    }  
} catch (Exception e) {  
    e.printStackTrace();  
}  

發佈了25 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章