Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Java 获取本机IP

Java 获取本机IP,Linux上也适用。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: " + arr[0]);
        System.out.println("localhost IP: " + 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());
            }
        }    }
   
   
}本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-03/128824.htm