JAVA如何获得外网IP地址

JAVA如何获得外网IP地址,第1张

java获取外网ip地址方法

public class Main {

public static void main(String[] args) throws SocketException {

Systemoutprintln(MaingetRealIp());

}

public static String getRealIp() throws SocketException {

String localip = null;// 本地IP,如果没有配置外网IP则返回它

String netip = null;// 外网IP

Enumeration<NetworkInterface> netInterfaces =

NetworkInterfacegetNetworkInterfaces();

InetAddress ip = null;

boolean finded = false;// 是否找到外网IP

while (netInterfaceshasMoreElements() && !finded) {

NetworkInterface ni = netInterfacesnextElement();

Enumeration<InetAddress> address = nigetInetAddresses();

while (addresshasMoreElements()) {

ip = addressnextElement();

if (!ipisSiteLocalAddress()

&& !ipisLoopbackAddress()

&& ipgetHostAddress()indexOf(":") == -1) {// 外网IP

netip = ipgetHostAddress();

finded = true;

break;

} else if (ipisSiteLocalAddress()

&& !ipisLoopbackAddress()

&& ipgetHostAddress()indexOf(":") == -1) {// 内网IP

localip = ipgetHostAddress();

}

}

}

if (netip != null && !""equals(netip)) {

return netip;

} else {

return localip;

}

}

}

为适应不同的网络情况,提供这个类:看下面的代码吧,Copy过去就能用。

import javaxservlet>

import javanet;

public class Test6 {

public static void main(String[] args) {

// TODO Auto-generated method stub

InetAddress ia=null;

try {

ia=iagetLocalHost();

String localname=iagetHostName();

String localip=iagetHostAddress();

Systemoutprintln("本机名称是:"+ localname);

Systemoutprintln("本机的ip是 :"+localip);

} catch (Exception e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

}

方法如下:

方法一,使用CMD命令:

public static String getLocalIPForCMD(){

StringBuilder sb = new StringBuilder();

String command = "cmdexe /c ipconfig | findstr IPv4";

try {

Process p = RuntimegetRuntime()exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(pgetInputStream()));

String line = null;

while((line = brreadLine()) != null){

line = linesubstring(linelastIndexOf(":")+2,linelength());

sbappend(line);

}

brclose();

pdestroy();

} catch (IOException e) {

eprintStackTrace();

}

return sbtoString();

}

方法二,使用Java方法

public static String getLocalIPForJava(){

StringBuilder sb = new StringBuilder();

try {

Enumeration<NetworkInterface> en = NetworkInterfacegetNetworkInterfaces();

while (enhasMoreElements()) {

NetworkInterface intf = (NetworkInterface) ennextElement();

Enumeration<InetAddress> enumIpAddr = intfgetInetAddresses();

while (enumIpAddrhasMoreElements()) {

InetAddress inetAddress = (InetAddress) enumIpAddrnextElement();

if (!inetAddressisLoopbackAddress() && !inetAddressisLinkLocalAddress()

&& inetAddressisSiteLocalAddress()) {

sbappend(inetAddressgetHostAddress()toString()+"\n");

}

}

}

} catch (SocketException e) { }

return sbtoString();

}

以上就是关于JAVA如何获得外网IP地址全部的内容,包括:JAVA如何获得外网IP地址、java如何获取当前登录ip、如何用 Java 获取系统 IP等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/10174328.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存