规则
127.xxx.xxx.xxx 属于 “loopback” 地址,即只能你自己的本机可见,就是本机地址,比较常见的有 127.0.0.1
192.168.xxx.xxx 属于 private 私有地址 (site local address),属于本地组织内部访问,只能在本地局域网可见
同样 10.xxx.xxx.xxx、从 172.16.xxx.xxx 到172.31.xxx.xxx 都是私有地址,也是属于组织内部访问
169.254.xxx.xxx 属于连接本地地址(link local IP),在单独网段可用
从 224.xxx.xxx.xxx 到 239.xxx.xxx.xxx 属于组播地址
比较特殊的 255.255.255.255 属于广播地址
除此之外的地址就是点对点的可用的公开 IPv4 地址
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| import org.springframework.util.ObjectUtils;
import java.net.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Optional;
public class IpUtil {
public static void main(String[] args) throws SocketException { System.out.println( IpUtil.getLocalIp4Address().get().toString().replaceAll("/","")); }
public static List<Inet4Address> getLocalIp4AddressFromNetworkInterface() throws SocketException { List<Inet4Address> addresses = new ArrayList<>(1);
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); if (ObjectUtils.isEmpty(networkInterfaces)) { return addresses; } while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); if (!isValidInterface(networkInterface)) { continue; }
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (isValidAddress(inetAddress)) { addresses.add((Inet4Address) inetAddress); } } } return addresses; }
private static boolean isValidInterface(NetworkInterface ni) throws SocketException { return !ni.isLoopback() && !ni.isPointToPoint() && ni.isUp() && !ni.isVirtual() && (ni.getName().startsWith("eth") || ni.getName().startsWith("en") || ni.getName().startsWith("ens")); }
private static boolean isValidAddress(InetAddress address) { return address instanceof Inet4Address && address.isSiteLocalAddress() && !address.isLoopbackAddress(); }
private static Optional<Inet4Address> getIpBySocket() throws SocketException { try (final DatagramSocket socket = new DatagramSocket()) { socket.connect(InetAddress.getByName("8.8.8.8"), 10002); if (socket.getLocalAddress() instanceof Inet4Address) { return Optional.of((Inet4Address) socket.getLocalAddress()); } } catch (UnknownHostException networkInterfaces) { throw new RuntimeException(networkInterfaces); } return Optional.empty(); }
public static Optional<Inet4Address> getLocalIp4Address() throws SocketException { final List<Inet4Address> inet4Addresses = getLocalIp4AddressFromNetworkInterface(); if (inet4Addresses.size() != 1) { final Optional<Inet4Address> ipBySocketOpt = getIpBySocket(); if (ipBySocketOpt.isPresent()) { return ipBySocketOpt; } else { return inet4Addresses.isEmpty() ? Optional.empty() : Optional.of(inet4Addresses.get(0)); } } return Optional.of(inet4Addresses.get(0)); } }
|