获取本地IP地址:
import java.net.InetAddress; import java.net.NetworkInterface; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; /** * @Description: ip工具类 * @Author: sbq * @CreateDate: 2019/8/8 10:33 * @Version: 1.0 */ public class IpUtil { /** * 获得服务器的IP地址 */ public static String getLocalIP() { String sIP = ""; InetAddress ip = null; try { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { bFindIP = true; break; } } } } catch (Exception e) { } if (null != ip) { sIP = ip.getHostAddress(); } return sIP; } /** * @Description: 获得服务器的IP地址(多网卡) * @Author: sbq * @CreateDate: 2019/8/8 10:34 * @Version: 1.0 */ public static List<String> getLocalIPS() { InetAddress ip = null; List<String> ipList = new ArrayList<String>(); try { Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().matches( "(\\d{1,3}\\.){3}\\d{1,3}")) { ipList.add(ip.getHostAddress()); } } } } catch (Exception e) { } return ipList; } }