首页 / JAVA / Java获取ip地址的几种方法
Java获取ip地址的几种方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java获取ip地址的几种方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3444字,纯文字阅读大概需要5分钟。
内容图文
以下内容介绍下java获取ip地址的几种思路。
1、直接利用java.net.InetAddress类获取,不过这种方法只在windows环境下有效,在linux环境下只能获取localhost地址(即/etc/hosts文件内容)
代码如下:
1 import java.net.InetAddress; 2 3 /** 4 * This method works well in windows system. 5 * In Linux system it returns 127.0.0.1 the content of the hosts file. 6 */ 7 public static void getIpAddressInWindows() { 8 try { 9 InetAddress address = InetAddress.getLocalHost(); 10 System.out.println("Host Name: " + address.getHostName()); 11 System.out.println("Host Address: " + address.getHostAddress()); 1213 } catch (UnknownHostException e) { 14 e.printStackTrace(); 15 } 16 }
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 interfacewhile (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 interfacewhile (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即可,可以通过如下方法获得:
1
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
/** 2 * This method is used to get ip address by network interface‘s name. 3 * @param networkInterfaceName network interface‘s name 4 * @return return true if get ip address successfully, 5 * otherwise return false. 6 */ 7 public static boolean getIpAddrByName(String networkInterfaceName) { 8 try { 9 // get network interface by name 10 NetworkInterface networkInterface = 11 NetworkInterface.getByName(networkInterfaceName); 12if (networkInterface == null) { 13returnfalse; 14 } 15 System.out.println("network interface: " + 16 networkInterface.getDisplayName()); 1718 InetAddress ipAddress = null; 19//get all ip addresses band to this interface20 Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); 2122while (addresses.hasMoreElements()) { 23 ipAddress = addresses.nextElement(); 2425if (ipAddress != null && ipAddress instanceof Inet4Address) { 26 System.out.println("ip address: " + 27 ipAddress.getHostAddress()); 28 } 29 } 30 } catch (SocketException e) { 31 e.printStackTrace(); 32 } 3334returntrue; 35 }// end method getIpAddrByName
原文:http://www.cnblogs.com/blog-zuo/p/4067018.html
内容总结
以上是互联网集市为您收集整理的Java获取ip地址的几种方法全部内容,希望文章能够帮你解决Java获取ip地址的几种方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。