package com.iapppay.mpisa.excel;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @className: AddressUtils
* @classDescription:
* @author lishiqiang
* @create_date: 2019年4月10日 上午10:27:02
* @update_date:
*/
public class AddressUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(AddressUtils.class);
public static String getLocalAddress() {
String ip =null;
try {
Enumeration<NetworkInterface> networkEnum = NetworkInterface.getNetworkInterfaces();
netlable: while (networkEnum.hasMoreElements()) {
NetworkInterface networkInterface = networkEnum.nextElement();
Enumeration<InetAddress> intAddressEnum = networkInterface.getInetAddresses();
while (intAddressEnum.hasMoreElements()) {
InetAddress inetAddress = intAddressEnum.nextElement();
if (inetAddress.isSiteLocalAddress() && !inetAddress.isLoopbackAddress()
&& !inetAddress.getHostAddress().contains(":")) {
ip = inetAddress.getHostAddress();
break netlable;
}
continue netlable;
}
}
} catch (SocketException e) {
LOGGER.info("get the address error");
}
return ip;
}
}
本文介绍了一个Java类,名为AddressUtils,用于在Java应用程序中获取本地计算机的IP地址。该类通过枚举所有网络接口并筛选出有效的IPv4地址来实现此功能,特别排除了环回地址和IPv6地址。
130

被折叠的 条评论
为什么被折叠?



