hc
2023-11-07 f45e756958099c35d6afb746df1d40a1c6302cfc
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
package com.rockchip.alexa.jacky.socket;
 
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.NetworkInterface;
import java.net.SocketException;
 
/**
 * LocalNet类
 * @author Jacky.ge
 *
 */
public class LocalNet {
 
    /**
     * IceWee 2017.03.11
     * 获取本地IP列表(针对多网卡情况)
     *
     * @return
     */
    private static List<String> getLocalIPList() {
        List<String> ipList = new ArrayList<String>();
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            NetworkInterface networkInterface;
            Enumeration<InetAddress> inetAddresses;
            InetAddress inetAddress;
            String ip;
            while (networkInterfaces.hasMoreElements()) {
                networkInterface = networkInterfaces.nextElement();
                inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    inetAddress = inetAddresses.nextElement();
                    if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
                        ip = inetAddress.getHostAddress();
                        ipList.add(ip);
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return ipList;
    }
 
    /** 
     * 获取本机的IP 
     * @return Ip地址 
     */
    public static String getLocalIP() {
        List<String> ipList = getLocalIPList();
        if (ipList == null || ipList.size() == 0)
            return "127.0.0.1";
 
        String ip;
        int size = ipList.size();
        for (int i=0; i<size; i++) {
            ip = ipList.get(i);
            if (ip.length() > 0 && !ip.trim().equals("127.0.0.1")) {
                return ip;
            }
        }
        return "127.0.0.1";
    }
}