以下内容介绍下java获取ip地址的几种思路。

1、直接利用java.net.InetAddress类获取,不过这种方法只在windows环境下有效,在linux环境下只能获取localhost地址(即/etc/hosts文件内容)

  代码如下: 

     import java.net.InetAddress;

     /**
* This method works well in windows system.
* In Linux system it returns 127.0.0.1 the content of the hosts file.
*/
public static void getIpAddressInWindows() {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("Host Name: " + address.getHostName());
System.out.println("Host Address: " + address.getHostAddress()); } catch (UnknownHostException e) {
e.printStackTrace();
}
}

2、可以在linux下正常工作的方法,代码如下:

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

    /**
* This method is used to get all ip addresses from the network interfaces.
* network interfaces: eth0, wlan0, l0, vmnet1, vmnet8
*/
public static void getAllIpAddress() {
try {
//get all network interface
Enumeration<NetworkInterface> allNetworkInterfaces =
NetworkInterface.getNetworkInterfaces();
NetworkInterface networkInterface = null; //check if there are more than one network interface
while (allNetworkInterfaces.hasMoreElements()) {
//get next network interface
networkInterface = allNetworkInterfaces.nextElement();
//output interface's name
System.out.println("network interface: " +
networkInterface.getDisplayName()); //get all ip address that bound to this network interface
Enumeration<InetAddress> allInetAddress =
networkInterface.getInetAddresses(); InetAddress ipAddress = null; //check if there are more than one ip addresses
//band to one network interface
while (allInetAddress.hasMoreElements()) {
//get next ip address
ipAddress = allInetAddress.nextElement();
if (ipAddress != null && ipAddress instanceof Inet4Address) {
System.out.println("ip address: " +
ipAddress.getHostAddress());
}
}
} } catch (SocketException e) {
e.printStackTrace();
}
}//end method getAllIpAddress

上边这种方法有些不足之处,它会输出所有的网卡上的ip地址,有时候我们只需一个或几个单独的网卡ip即可,可以通过如下方法获得:    


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

       /**
* This method is used to get ip address by network interface's name.
* @param networkInterfaceName network interface's name
* @return return true if get ip address successfully,
* otherwise return false.
*/
public static boolean getIpAddrByName(String networkInterfaceName) {
try {
        //get network interface by name
NetworkInterface networkInterface =
NetworkInterface.getByName(networkInterfaceName);
if (networkInterface == null) {
return false;
}
System.out.println("network interface: " +
networkInterface.getDisplayName()); InetAddress ipAddress = null;
//get all ip addresses band to this interface
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) {
ipAddress = addresses.nextElement(); if (ipAddress != null && ipAddress instanceof Inet4Address) {
System.out.println("ip address: " +
ipAddress.getHostAddress());
}
}
} catch (SocketException e) {
e.printStackTrace();
} return true;
}// end method getIpAddrByName

Java获取ip地址的几种方法的更多相关文章

  1. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  2. php 获取ip地址的5种方法,插入用户登录日志实例

    php 获取ip地址的5种方法,插入用户登录日志实例,推荐使用第二种方法 <?php //方法1: $ip = $_SERVER["REMOTE_ADDR"]; echo $ ...

  3. 获取IP地址的几种方法

    根据ip获取地址的几种方法 1.调用新浪IP地址库 <script type="text/javascript" src="js/jquery.js"&g ...

  4. php获取客户端IP地址的几种方法(转)

    [php] view plain copy php获取客户端IP地址的几种方法 方法一 <?php $iipp=$_SERVER["REMOTE_ADDR"]; echo $ ...

  5. C#获取MAC地址的几种方法

    首先需要用到的一些方法和类: public enum NCBCONST { NCBNAMSZ = 16, MAX_LANA = 254, NCBENUM = 0x37, NRC_GOODRET = 0 ...

  6. java获取IP地址

    最近在一个多系统集成的项目中,由于跳转路径含IP地址,每次IP改了重启项目都得改好多地方,甚是麻烦.刚在网上了解到java获取IP地址,给大家分享下: 首先要导入jar包 request.getRem ...

  7. CentOS 7配置静态IP地址的两种方法 来自:互联网

    CentOS 7配置静态IP地址的两种方法 来自:互联网 时间:2021-01-12 阅读:4 如果你想要为CentOS 7中的某个网络接口设置静态IP地址,有几种不同的方法,这取决于你是否想要使用网 ...

  8. 树莓派4B获取IP地址的几种简易方法

    首先声明一下,使用的是Paspbian系统,其实其他系统和本文说的获取IP地址关系也不大. 1.当你有路由器,有PC客户端的情况,你把你的树莓派用网线将其连接起来.你可以借助这个软件,advanced ...

  9. 【转载】VC获取MAC地址的4种方法

    From:http://blog.csdn.net/pdfmaker/article/details/465748 有需求才有创造,有了问题才会想着去解决,那么我这里的获取MAC地址的第4种方法也是在 ...

随机推荐

  1. OpenERP新手易犯错误之res.model

    接触OpenERP的人都感慨资料之少,尤其是XML中,出点错是相当郁闷的.尤其是新手.什么都别说了,有图有真相. 视图中关联模型name="model" ,而动作中name=&qu ...

  2. __block的作用

    __block 的标记告诉编译器,这个变量在 block 里面需要做特殊处理. 一般来说,在 block 中用的变量值是被复制过来的,所以对于变量本身的修改并不会影响这个变量的真实值.而当我们用 __ ...

  3. 【JS跨域请求】Ajax跨域请求JSONP

    前两天被问到ajax跨域如何解决,还真被问住了,光知道有个什么jsonp,迷迷糊糊的没有说上来.抱着有问题必须解决的态度,我看了许多资料,原来如此... 为何一直知道jsonp,但一直迷迷糊糊的不明白 ...

  4. php 自定义求数组差集,效率比自带的array_diff函数还要快(转)

    <?phpfunction array_different($array_1, $array_2) { $array_2 = array_flip($array_2); //将数组键值调换 fo ...

  5. 从零开始写一个Tomcat(叁)--请求解析

    挖坑挖了这么长时间也该继续填坑了,上文书讲到从零开始写一个Tomcat(贰)--建立动态服务器,讲了如何让服务器解析请求,分离servlet请求和静态资源请求,读取静态资源文件输出或是通过URLCla ...

  6. select poll epoll三者之间的比较

    一.概述 说到Linux下的IO复用,系统提供了三个系统调用,分别是select poll epoll.那么这三者之间有什么不同呢,什么时候使用三个之间的其中一个呢? 下面,我将从系统调用原型来分析其 ...

  7. JDK5-注解

    注解是一个类,一个声明就是一个对象 1. 简单注解 @SuppressWarnings("msg") - 压缩/抑制警告 @Deprecated - 声明过时 @Override ...

  8. linux命令行计算器 <转>

    转自 http://blog.chinaunix.net/uid-26959241-id-3207711.html 详细文档请 man bc 在windows下,大家都知道直接运行calc,(c:\w ...

  9. ARM map(Program size)

    1.Keil程式编译完之后,在List目录下会生成一个.map文件,里面包含各个存储块数据大小. Code:ARM 指令. RO(Read only)只读数据,如const int gu8test = ...

  10. uva 10934 Dropping water balloons(转载)

    本文转载自http://blog.csdn.net/shuangde800/article/details/11273123 题意 你有k个一模一样的水球,在一个n层楼的建筑物上进行测试,你想知道水球 ...