Java獲取本地IP和計算機名
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test {
public static void main(String[] args) {
try {
InetAddress addr = InetAddress.getLocalHost();
String ip ,address;//獲得本機IP
ip = ip=addr.getHostAddress().toString();
address=addr.getHostName().toString();
System.out.println(ip+" "+address);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}