public static String getIPAddress(boolean useIPv4) {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : interfaces) {
List<InetAddress> addresses = Collections.list(networkInterface.getInetAddresses());
for (InetAddress address : addresses) {
if (!address.isLoopbackAddress()) {
String sAddr = address.getHostAddress();
boolean isIPv4 = sAddr.indexOf(':') < 0;
if (useIPv4) {
if (isIPv4) {
return sAddr;
}
} else {
if (!isIPv4) {
int delim = sAddr.indexOf('%');
return delim < 0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
}
}
}
}
}
}catch (Exception ignored) {
}
return "";
}