廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
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
|
public String getLocalHostName() { String hostName; try { InetAddress addr = InetAddress.getLocalHost(); hostName = addr.getHostName(); } catch (Exception ex) { hostName = "" ; } return hostName; } public String[] getAllLocalHostIP() { String[] ret = null ; try { String hostName = getLocalHostName(); if (hostName.length() > 0 ) { InetAddress[] addrs = InetAddress.getAllByName(hostName); if (addrs.length > 0 ) { ret = new String[addrs.length]; for ( int i = 0 ; i < addrs.length; i++) { ret[i] = addrs[i].getHostAddress(); } } } } catch (Exception ex) { ret = null ; } return ret; } |
以上代碼是小編給大家介紹的java獲取多網卡本地ip的相關代碼,有疑問歡迎給我留言,我會及時和大家溝通,共同學習進步,同時也非常感謝大家對服務器之家網站的支持!