发布网友 发布时间:2022-04-24 17:34
共2个回答
热心网友 时间:2022-05-03 13:43
《java》中提取本地IP的方法如下:
private static void getIpAddressByNetworkInterface() {
try {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
NetworkInterface net;
InetAddress inetAddress;
while (nets.hasMoreElements()) {
net = nets.nextElement();
Enumeration<InetAddress> address = net.getInetAddresses();
while (address.hasMoreElements()) {
inetAddress = address.nextElement();
if (inetAddress!=null&&inetAddress instanceof Inet4Address)
System.out.println(inetAddress.getHostAddress());
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
热心网友 时间:2022-05-03 15:01
System.out.println(InetAddress.getByName("www.sina.com.cn"));
import java.net.*;
public class Test {
public static void main(String[] args) throws Exception {
String ip = InetAddress.getLocalHost().getHostAddress();
System.out.println(ip);
}
}