Java获取ip地址的几种方法
- 作者: 五速梦信息网
- 时间: 2026年04月04日 13:36
以下内容介绍下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下正常工作的方法,代码如下:
/**
* 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即可,可以通过如下方法获得:
/**
* 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基础(一):谈谈java内存管理与垃圾回收机制
java基础(一):谈谈java内存管理与垃圾回收机制
- 互联网
- 2026年04月04日
-
Java基础学习总结(40)——Java程序员最常用的8个Java日志框架
Java基础学习总结(40)——Java程序员最常用的8个Java日志框架
- 互联网
- 2026年04月04日
-
java基础之——DecimalFormat格式化数字
java基础之——DecimalFormat格式化数字
- 互联网
- 2026年04月04日
-
JAVA环境变量和TomCat服务器配置
JAVA环境变量和TomCat服务器配置
- 互联网
- 2026年04月04日
-
java后台如何获取国际化中的key
java后台如何获取国际化中的key
- 互联网
- 2026年04月04日
-
java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
- 互联网
- 2026年04月04日






