java中通过网卡名称获取IP地址

2016-02-19 10:35 41 1 收藏

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的java中通过网卡名称获取IP地址,过去的都会过去,迎接崭新的开始,释放更美好的自己。

【 tulaoshi.com - 编程语言 】

代码如下:

package me.xuzs.sso.test;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class InternetTest {

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

    public static void main(String[] args) {
        String netCard = "lo";
        try {
            EnumerationNetworkInterface netInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            if (netInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = netInterfaces.nextElement();
                if (netCard.equals(netInterface.getName())) {
                    // 子接口,linux下会取到父接口??
                    EnumerationNetworkInterface subnetInterfaces = netInterface
                            .getSubInterfaces();
                    while (subnetInterfaces.hasMoreElements()) {
                        NetworkInterface subnetInterface = subnetInterfaces
                                .nextElement();
                        System.out.println(subnetInterface.getName());
                        EnumerationInetAddress subaddresses = netInterface
                                .getInetAddresses();
                        while (subaddresses.hasMoreElements()) {
                            InetAddress subaddress = subaddresses.nextElement();
                            System.out.println(subaddress.getHostAddress());
                        }
                    }
                    // 打印接口下所有IP
                    System.out.println(netInterface.getName());
                    EnumerationInetAddress addresses = netInterface
                            .getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        System.out.println(address.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1595155.html

延伸阅读
代码如下:    package net;  import java.net.*;  /*   *  getAddress方法和getHostAddress类似,它们的唯一区别是getHostAddress方法返回的是字符串形式的IP地址,   *  而getAddress方法返回的是byte数组形式的IP地址。   *  Java中byte类型的取值范围是-128?127。如果返回的IP地址的...
tomcat本地地址 E:soft4developapache-tomcat-6.0.18 System.getProperty("user.dir")//E:soft4developapache-tomcat-6.0.18bin System.getProperty("catalina.home")//E:soft4developapache-tomcat-6.0.18对于jboss同样适用。其他容器未做测试。 说道这里,正好有朋友在群里头问了个问题,情景式这样的 登陆需要用Https...
在Windows编程环境中,我们有一个可以调用的API 函数GetHostByName ,以前在Asp中我们必须通过组件来实现 类似的功能,比如AspDNS等等,现在MS在ASP.Net上给我们提供了新的编程接口,让我们可以很轻松的实现这样类似 的功能.原因是在Asp.Net中给我们提供了一个DNS Class,我们可以通过这个Class 轻松的实现我们在上面所提到的 功能。 我们可以看到...
标签: Web开发
最近没事,写了个在项目经常要取城市或省份名的方法,所以改成了一个类.方便以后调用 //******************************************************************************** //*                      &n...
     我们要使用ADO.NET 中的对象,必需先宣告ADO.NET 的名称地址(Namespace)。因为微软为.NET 做了数量相当可观的类别对象,所以宣告名称地址是必要的。名称地址中记录了对象的名称以及所在,这样编译器在编译我们程序的时候才知道这些对象要到哪里去加载,和VB 6的设定Reference 很类似。 &nb...

经验教程

111

收藏

17
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部