You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
|
4 years ago
|
package com.ynxbd.push.helper;
|
||
|
|
|
||
|
|
import java.net.Inet4Address;
|
||
|
|
import java.net.InetAddress;
|
||
|
|
import java.net.NetworkInterface;
|
||
|
|
import java.util.Enumeration;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取本机ip地址
|
||
|
|
*
|
||
|
|
* @author 李进才
|
||
|
|
*/
|
||
|
|
public class IPHelper {
|
||
|
|
private IPHelper() {
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public synchronized static String getIp() throws NullPointerException {
|
||
|
|
Enumeration<NetworkInterface> allNetInterfaces; // networkInterfaces
|
||
|
|
InetAddress ip;
|
||
|
|
|
||
|
|
try {
|
||
|
|
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
|
||
|
|
while (allNetInterfaces.hasMoreElements()) {
|
||
|
|
NetworkInterface netInterface = allNetInterfaces.nextElement();
|
||
|
|
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
|
||
|
|
while (addresses.hasMoreElements()) {
|
||
|
|
ip = addresses.nextElement();
|
||
|
|
if (ip instanceof Inet4Address) {
|
||
|
|
if (ip.getHostAddress().equals("127.0.0.1")) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
return ip.getHostAddress();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (java.net.SocketException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|