如何从java applet获取本地机器ip

时间:2014-03-03 06:56:27

标签: java applet

嗨我正在使用以下代码从java applet获取本地机器ip 但我总是得到127.0.0.1而不是实际的IP

public String ip;
public void init()
{
    try
    {
        Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
        for (; n.hasMoreElements();)
        {
            NetworkInterface e = n.nextElement();

            Enumeration<InetAddress> a = e.getInetAddresses();
            for (; a.hasMoreElements();)
            {
                InetAddress addr = a.nextElement();
                ip = "Really " + addr.getHostAddress();
                System.out.println(ip);
            }
        } 
    }
    catch(Exception ex)
    {
    }       
}

4 个答案:

答案 0 :(得分:1)

试试这个样本:

import java.net.InetAddress;
...

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

我在这里找到了这个解决方案:How to get Server IP address in Java 它适用于我的。

答案 1 :(得分:0)

您无法通过未签名的小程序获取此信息,但您可以向applet授予相应的政策。首先阅读this以获取有关设置策略文件的一般说明。您需要授予"getNetworkInformation" permission。您可能需要签名。

完成这项工作后,您可以使用NetworkInterface获取界面信息。仅检查活动接口,并跳过环回和链接本地地址,例如:

Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
    NetworkInterface iface = ifaces.nextElement();
    if (iface.isUp()) {
        Enumeration<InetAddress> addrs = iface.getInetAddresses();
        while (addrs.hasMoreElements()) {
            InetAddress addr = addrs.nextElement();
            if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress())
                System.out.println(addr.getHostAddress());
        }
    }
}

请记住,如果存在多个接口,则可能存在多个IP。您还将获得IPv4和IPv6地址。您可以通过检查InetAddressInet4Address还是Inet6Address的实例来过滤协议,例如:

if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && (addr instanceof Inet4Address))
    ...; // IPv4 addresses only.

答案 2 :(得分:0)

可能是您更容易接受的脚本解决方案吗?

像这样:

<script>
var myip;
function ip_callback(o) {
    ip = o.host;
}
</script>
<script src="https://smart-ip.net/geoip-json?callback=ip_callback"></script>
<script>alert(ip);</script>

在这种情况下,此主题将非常有用:Get client IP address via third party web service

答案 3 :(得分:0)

您可以检查网络地址是否为instanceof Inet4Address。如果它返回true,你可以选择相同的。

示例1

InetAddress ia = InetAddress.getLocalHost();
System.out.println( "ia.getLocalHost(): " + ia );
System.out.println( "ia.getHostAddress(): " + ia.getHostAddress() );

// true for 192.168.1.193 or 127.0.0.1
boolean isIpv4 = ( ia instanceof Inet4Address );

// true for 2001:0:9d38:6abd:186b:38da:3f57:fe3e
boolean isIpv6 = ( ia instanceof Inet6Address );

如果getHostAddress()返回127.0.0.1,最好使用NetworkInterface来识别本地IP地址,如果网络接口已启动,您可以从中找到IP4地址。

示例2

Enumeration<NetworkInterface> enumNwi = NetworkInterface.getNetworkInterfaces();
while( enumNwi.hasMoreElements() ) {
    NetworkInterface nwi = enumNwi.nextElement();
    if ( nwi.isUp() ) { // if the nw is up and running
        Enumeration<InetAddress> enumInetAddresses = nwi.getInetAddresses();
        while ( enumInetAddresses.hasMoreElements() ) {
            InetAddress inetAddress = enumInetAddresses.nextElement();
            // check if it is not 127.0.0.1
            if ( ! inetAddress.isLinkLocalAddress() &&
                 ! inetAddress.isLoopbackAddress() ) {
                System.out.println( inetAddress.getHostAddress() + 
                                    ( inetAddress instanceof Inet4Address ?
                                      " <<------ IP4 address" : "" ) );
            }
        }
    }
}

请参阅