| | |
| | | package com.ruoyi.common.utils.ip; |
| | | |
| | | import java.net.InetAddress; |
| | | import java.net.UnknownHostException; |
| | | import java.net.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Enumeration; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import com.ruoyi.common.utils.ServletUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | /** |
| | | * 获取IP方法 |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Slf4j |
| | | public class IpUtils |
| | | { |
| | | public final static String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"; |
| | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | private static final String LOOP_BACK = "127.0.0.1"; |
| | | private static String firstNoLoopbackIPV4Address = null; |
| | | |
| | | private static Collection<InetAddress> allHostIPV4Address = null; |
| | | /** |
| | | * 获取ipv4地址,如果有多个网卡的情况,获取第一个非loopback ip地址 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getFirstNoLoopbackIPV4Address() { |
| | | if (firstNoLoopbackIPV4Address != null) { |
| | | return firstNoLoopbackIPV4Address; |
| | | } |
| | | Collection<String> allNoLoopbackAddresses = null; |
| | | try { |
| | | allNoLoopbackAddresses = getAllNoLoopbackIPV4Addresses(); |
| | | } catch (Exception e) { |
| | | log.error("获取ip失败", e); |
| | | return LOOP_BACK; |
| | | } |
| | | if (allNoLoopbackAddresses.isEmpty()) { |
| | | return LOOP_BACK; |
| | | } |
| | | |
| | | return firstNoLoopbackIPV4Address = allNoLoopbackAddresses.iterator().next(); |
| | | } |
| | | |
| | | |
| | | public static Collection<String> getAllNoLoopbackIPV4Addresses() { |
| | | Collection<String> noLoopbackAddresses = new ArrayList<String>(); |
| | | Collection<InetAddress> allInetAddresses = getAllHostIPV4Address(); |
| | | |
| | | for (InetAddress address : allInetAddresses) { |
| | | if (!address.isLoopbackAddress()) { |
| | | noLoopbackAddresses.add(address.getHostAddress()); |
| | | } |
| | | } |
| | | |
| | | return noLoopbackAddresses; |
| | | } |
| | | |
| | | public static Collection<InetAddress> getAllHostIPV4Address() { |
| | | if (allHostIPV4Address == null) { |
| | | try { |
| | | Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); |
| | | Collection<InetAddress> addresses = new ArrayList<InetAddress>(); |
| | | |
| | | while (networkInterfaces.hasMoreElements()) { |
| | | NetworkInterface networkInterface = networkInterfaces.nextElement(); |
| | | Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); |
| | | while (inetAddresses.hasMoreElements()) { |
| | | InetAddress inetAddress = inetAddresses.nextElement(); |
| | | if (inetAddress instanceof Inet4Address) { |
| | | addresses.add(inetAddress); |
| | | } |
| | | } |
| | | } |
| | | allHostIPV4Address = addresses; |
| | | } catch (SocketException e) { |
| | | log.error("获取ip地址失败", e); |
| | | throw new RuntimeException(e.getMessage(), e); |
| | | } |
| | | |
| | | } |
| | | return allHostIPV4Address; |
| | | } |
| | | } |