java linux获取本机ip_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:\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());

}

}

}

}

java linux获取本机ip_Java 获取本机IP_第1张图片

0b1331709591d260c1c78e86d0c51c18.png

你可能感兴趣的:(java,linux获取本机ip)