JavaでローカルIPアドレスを取得する
Javaコード:
try {
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
continue;
}
List<InterfaceAddress> addresses = networkInterface.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress : addresses) {
String ip = interfaceAddress.getAddress().getHostAddress();
if (ip.length() < 20) {
System.out.println(“IP:" + ip);
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}