以下内容介绍下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. IOS多线程的小总结

    ios中多线程的实现方案有4种    1.pthread :一套通用的多线程API/适用于Unix\Linux\Windows等系统 (跨平台可移植/使用难度大)    C语言    几乎不用     ...

  2. ubuntu firefox 选中变成了删除

    在ubuntu下 我的firefox浏览器出现了问题.描述: 1.在firefox中所有能够删除的文字只要选中就自动删除了. 终端中 ibus-setup勾掉在应用窗口中启用内嵌编辑模式

  3. [Javascript] Writing conventional commits with commitizen

    Because semantic-release requires a specific message format, it's easier to follow this convention u ...

  4. poj2993 poj2669

    扯淡题. 2993 #include <iostream> #include <stdio.h> #include <string> #include <st ...

  5. Ubuntu install mysql

    sudo apt-get install mysql-server sudo apt-get install libmysqlclient-dev

  6. POJ 1986(LCA and RMQ)

    题意:给定一棵树,求任意两点之间的距离. 思路:由于树的特殊性,所以任意两点之间的路径是唯一的.u到v的距离等于dis(u) + dis(v) - 2 * dis(lca(u, v)); 其中dis( ...

  7. Android定时器功能实现方法

    在Android开发中,定时器一般有以下3种实现方法: 1.采用Handler与线程的sleep(long)方法 2.采用Handler的postDelayed(Runnable, long)方法 3 ...

  8. sql查阅每一月的数据

    因为项目中需要做数据报表的功能,需要统计每个月的销售额.我找到下面的sql语句.后来经过自己的测试,发现第二句才是可以用的, //String sql="SELECT year(buydat ...

  9. Wpf 数据绑定简介、实例1

    简介:1.WPF绑定使用的源属性必须是依赖项属性,这是因为依赖项属性具有内置的更改通知支持,元素绑定表达式使用了Xaml扩展标记, WPF绑定一个控件是使用Binding.ElementName, 绑 ...

  10. iOSUI基础——懒加载

    1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 ...