123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package net.lab1024.smartadmin.common.heartbeat;
- 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;
- }
- }
|