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