使用Java的IP地址

时间:2012-08-13 19:34:42

标签: java ip ip-address

我如何获得用户的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 个答案:

答案 0 :(得分:7)

最短的方法是:

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

然而,getLocalHost文档说:

  

如果有安全管理器,则调用其checkConnect方法   本地主机名和-1作为其参数,以查看操作是否   允许。如果不允许该操作,则表示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();
    }

<强>参考

答案 1 :(得分:1)

当您致电getLocalHost()时,您要求提供您所连接的路由器的相对地址,这是(正如预期的那样)127.0.0.1。要使用InetAddress确定IP地址,请尝试:

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

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

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

答案 2 :(得分: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());

答案 3 :(得分:-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);
}