废话不多说了,直接给大家贴代码了,具体代码如下所示:
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的相关代码,有疑问欢迎给我留言,我会及时和大家沟通,共同学习进步,同时也非常感谢大家对服务器之家网站的支持!