《java》中提取本地IP的方法如下:
private static void getIpAddressByNetworkInterface() {
try {
Enumeration
NetworkInterface net;
InetAddress inetAddress;
while (nets.hasMoreElements()) {
net = nets.nextElement();
Enumeration
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();
}
}
System.out.println(InetAddress.getLocalHost().getHostAddress());
System.out.println(InetAddress.getByName("www.sina.com.cn"));
Socket client = new Socket("192.168.6.8", 80);
System.out.println(client.getInetAddress().getHostAddress());
import java.net.*;
public class Test {
public static void main(String[] args) throws Exception {
String ip = InetAddress.getLocalHost().getHostAddress();
System.out.println(ip);
}
}