4

如何获取用户 IP 地址?

InetAddress ip;
try {

 ip = InetAddress.getLocalHost();
 System.out.println("Current IP address : " + ip.getHostAddress());

} catch (UnknownHostException e) {

e.printStackTrace();

 }

返回:127.0.0.1

我知道那不是我的IP。这是我的本地IP地址。如何使用 java.. 获取用户 IP?

4

4 回答 4

7

最短的方法是:

try {
InetAddress thisIp =InetAddress.getLocalHost();
System.out.println("IP:"+thisIp.getHostAddress());
}
catch(Exception e) {
e.printStackTrace();
}

但是 getLocalHost文档说:

如果有安全管理器,则使用本地主机名和 -1 作为其参数调用其 checkConnect 方法,以查看是否允许该操作。如果不允许该操作,则返回表示环回地址的 InetAddress。

并且在某些情况下InetAddress.getLocalHost()不咨询您的接口,它只是返回常量 127.0.0.1(对于 IPv4))

我认为NetworkInterface.getNetworkInterfaces你需要列举所有的可能性。这是一个不显示虚拟地址但适用于“主”接口的示例:

import java.net.*;
import java.util.*;

public class Test
{
    public static void main(String[] args)
        throws Exception // Just for simplicity
    {
        for (Enumeration<NetworkInterface> ifaces = 
               NetworkInterface.getNetworkInterfaces();
             ifaces.hasMoreElements(); )
        {
            NetworkInterface iface = ifaces.nextElement();
            System.out.println(iface.getName() + ":");
            for (Enumeration<InetAddress> addresses =
                   iface.getInetAddresses();
                 addresses.hasMoreElements(); )
            {
                InetAddress address = addresses.nextElement();
                System.out.println("  " + address);
            }
        }
    }
}

或者:

尝试使用这个(第一个输出应该是 PC 名称后的 IP):

    InetAddress[] localaddr;

    String computername = null;

    try {
        computername = InetAddress.getLocalHost().getHostName();//get pc name
    } catch (UnknownHostException ex) {
        ex.printStackTrace();
    }

    System.out.println(computername);

    try {
        localaddr = InetAddress.getAllByName(computername);
        for (int i = 0; i < localaddr.length; i++) {
            System.out.println("\n" + localaddr[i].getHostAddress());
        }
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

参考资料

于 2012-08-13T19:38:13.163 回答
1

当您打电话时,getLocalHost()您正在询问您所连接的路由器的相对地址,即(如预期的那样)127.0.0.1。要使用 确定 IP 地址InetAddress,请尝试:

InetAddress.getByName("http://yoururl.com/path/");

还有一种getAllByName(String)方法可以为您的目的服务。阅读 javadoc。

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html#getHostAddress ()

于 2012-08-13T19:42:40.837 回答
0

它获取您机器的IP地址,

    InetAddress inet= InetAddress.getLocalHost();
    String str[]=inet.toString().split("/");
    for (int i=0;i<str.length;i++)
    System.out.println(inet.toString() +" "+ str[i]);
    InetAddress[] inetAd=InetAddress.getAllByName(str[0]);
    for(int j=0;j<inetAd.length;j++ ){
    System.out.println(inetAd[j].toString());
于 2013-12-02T16:27:10.460 回答
-1

这将返回您当前使用的接口地址:

NetworkInterface ni;
try {
    Enumeration<NetworkInterface> interfaces NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        ni = interfaces.nextElement();
        if (ni.isUp()) {
            Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress ia = inetAddresses.nextElement();
                if (!ia.isLinkLocalAddress()) {
                    return ia;
                }
            }
        }
    }
} catch (SocketException ex) {
    throw new RuntimeException(ex);
}
于 2012-08-13T22:59:14.047 回答