用Java实现IP地址
如何获取用户的IP地址用Java实现IP地址,java,ip,ip-address,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
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?最短的方法是:
try {
InetAddress thisIp =InetAddress.getLocalHost();
System.out.println("IP:"+thisIp.getHostAddress());
}
catch(Exception e) {
e.printStackTrace();
}
但是getLocalHost
文档说:
如果存在安全管理器,则使用调用其checkConnect方法
本地主机名和-1作为其参数,以查看操作是否正确
允许。如果不允许该操作,则表示
返回环回地址
在某些情况下,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);
}
}
}
}
import java.net.*;
导入java.util.*;
公开课考试
{
公共静态void main(字符串[]args)
抛出异常//只是为了简单起见
{
对于(枚举ifaces=
NetworkInterface.getNetworkInterfaces();
ifaces.hasMoreElements();)
{
网络接口iface=ifaces.nextElement();
System.out.println(iface.getName()+“:”);
用于(枚举地址)=
iface.getInetAddresses();
addresses.hasMoreElements();)
{
InetAddress=addresses.nextElement();
System.out.println(“+”地址);
}
}
}
}
或者:
尝试使用此选项(第一个输出应为PC名称后的IP):
InetAddress[]localaddr;
字符串computername=null;
试一试{
computername=InetAddress.getLocalHost().getHostName();//获取pc名称
}捕获(未知后异常除外){
例如printStackTrace();
}
System.out.println(计算机名);
试一试{
localaddr=InetAddress.getAllByName(计算机名);
for(int i=0;i
参考资料:
getLocalHost()
时,您正在询问所连接路由器的相对地址,即(如预期的)127.0.0.1
。要使用InetAddress
确定IP地址,请尝试:
InetAddress.getByName("http://yoururl.com/path/");
还有一个getAllByName(String)
方法可以满足您的需要。阅读javadoc
()这将返回您当前使用的接口地址:
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);
}
NetworkInterface-ni;
试一试{
枚举接口NetworkInterface.getNetworkInterfaces();
while(interfaces.hasMoreElements()){
ni=interfaces.nextElement();
if(ni.isUp()){
枚举inetAddresses=ni.getInetAddresses();
while(inetAddresses.hasMoreElements()){
InetAddress ia=inetAddresses.nextElement();
如果(!ia.isLinkLocalAddress()){
返回ia;
}
}
}
}
}捕获(SocketException例外){
抛出新的运行时异常(ex);
}
它获取您机器的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());
InetAddress inet=InetAddress.getLocalHost();
字符串str[]=inet.toString().split(“/”);
对于(int i=0;我希望我的IP是:xx.xx.x.xxx,这是输出中的第三行。@HenryHarris我不确定……上述方法所做的是遍历所有网络接口并打印出它们的地址。找出你的IP,然后检查你是否在输出中看到它。在IP查找中找不到我的IP..O.O。
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());