以下内容介绍下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

最新文章

  1. ionic ios发布图标启动也不能正确加载问题
  2. Redis Lua脚本原理
  3. 表单脚本api_contenteditable
  4. Redis实现分布式锁
  5. HTML语言的一些元素(二)
  6. SpiderMonkey-让你的C++程序支持JavaScript脚本
  7. UpdatePanel无法直接弹出窗口的解决
  8. iOS之NSURLSessionDownloadTask下载
  9. 修复ubuntu播放wmv等视频没有声音问题
  10. oracle数据库、客户端安装以及ps/sql连接和导入表实例
  11. asp.net mvc 接入阿里大于 短信验证码发送
  12. Asp.net实现下拉框和列表框的连动
  13. .NET和PHP程序员如何通过技术快速变现
  14. 华为oj之字符串最后一个单词的长度
  15. docker登录运行中的容器的4方案
  16. myEclipse全局搜索时报错
  17. React文档(六)state和生命周期
  18. system.setProperties
  19. [POC]K8 DLLhijack Test
  20. 用Maven构建Mahout项目

热门文章

  1. Spring与SpringMVC的容器关系分析
  2. Geometric Shapes (poj3449多边形相交)
  3. ss sp行情
  4. 另一个有趣的Captcha 网站
  5. 如何从硬盘安装fedora 19 (How to install fedora 19 from hard drive, Fedora-19-i386-DVD.iso)
  6. ping and traceroute(tracert)
  7. GIT使用指南
  8. myeclipse html乱码
  9. C#HttpWebResponse请求常见的状态码
  10. Tab页签切换