以下内容介绍下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. hdu 3698 Let the light guide us(线段树优化&简单DP)

    Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/O ...

  2. Linux下报 java.net.SocketException权限不够 异常解决

    转载自:http://wangchongan.com/articles/java-net-socket-exception-permission-denied.html 今天在Linux下用Jetty ...

  3. JAVA大数类

    JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...

  4. Android的Touch系统简介(一

    一.Android touch事件的相关概念 用户的Touch事件被包装成MotionEvent 用户当前的touch事件主要类型有: ACTION_DOWN: 表示用户开始触摸. ACTION_MO ...

  5. Linux开发工具之gdb(上)

    三.gdb调试(上) 01.gdb:gdb是GNU debugger的缩写,是编程调试工作. 功能:   启动程序,可以按照用户自定义的要求随心所欲的运行程序:   可让被调试的程序在用户所指定的调试 ...

  6. MediaCodec文档翻译

    MediaCodec|文档翻译 classoverView mediacodec类可以用来调用系统底层的编码/解码软件. mediacodec一般是这么用的: MediaCodec codec = M ...

  7. Android客户端与服务端交互之登陆示例

    Android客户端与服务端交互之登陆示例 今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的 1.后台 ...

  8. Sql语句 不支持中文 国外数据库

    由于老美的不支持中文 SQL 语句第一:字段类型改为nvarchar,ntext 第二:强制转化 N update dbo.Role set rolename=N'普通用户' update dbo.T ...

  9. call, apply, bind作用

    call, apply作用就是(改变方法中的this指向)借用别人的方法来调用,就像调用自己的一样 function Person(name) { this.name = name; } Person ...

  10. 织梦DeDeCms列表分页和内容页分页错位解决办法

    文章页分页代码在这里/include/arc.archives.class.php列表页分页/include/arc.listview.class.php 很多入门的站长会碰到这样的问题,织梦的通病, ...