「java获得公网ip」java web发布到公网
今天给各位分享java获得公网ip的知识,其中也会对java web发布到公网进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
JAVA获取外网IP
请参考以下代码,可以获得任意给定网卡的ip地址:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class InternetTest {
public static void main(String[] args) {
String netCard = "lo";
try {
EnumerationNetworkInterface netInterfaces = NetworkInterface
.getNetworkInterfaces();
if (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
if (netCard.equals(netInterface.getName())) {
// 子接口,linux下会取到父接口??
EnumerationNetworkInterface subnetInterfaces = netInterface
.getSubInterfaces();
while (subnetInterfaces.hasMoreElements()) {
NetworkInterface subnetInterface = subnetInterfaces
.nextElement();
System.out.println(subnetInterface.getName());
EnumerationInetAddress subaddresses = netInterface
.getInetAddresses();
while (subaddresses.hasMoreElements()) {
InetAddress subaddress = subaddresses.nextElement();
System.out.println(subaddress.getHostAddress());
}
}
// 打印接口下所有IP
System.out.println(netInterface.getName());
EnumerationInetAddress addresses = netInterface
.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
System.out.println(address.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
怎么才能获得公网IP?
1、登陆路由器。如图所示。
2、然后我们就能在运行状态里面看到Wan的IP地址了。如图所示。
3、WAN的IP地址就是我们用于上网的IP地址了。切换到系统工具。如图所示。
4、当然我们也可以在其他地方看公网IP。
5、切换到系统工具。如图所示。
6、然后打开系统日志,在系统日志里面有一个W=什么什么的,这个就是公网的IP地址了。如图所示。
Java怎样获取当前机器外网IP
java获取本机的外网ip示例:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
Java编程 怎样获得自己在外网的真实ip
其实就是在外网搞一个服务器,然后用java去访问这个服务器,然后服务器就能获取到你的外网ip,只需要对数据进行解析。服务器可以自己架设或者网上也有很多提供这样服务的网站,如各种的查ip的网站都有显示自己外网ip的功能。下边是个例子,可以直接运行
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class ListIP {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("本机的外网IP是:"+ListIP.getWebIp(""));
}
public static String getWebIp(String strUrl) {
try {
URL url = new URL(strUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url
.openStream()));
String s = "";
StringBuffer sb = new StringBuffer("");
String webContent = "";
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
}
br.close();
webContent = sb.toString();
int start = webContent.indexOf("[")+1;
int end = webContent.indexOf("]");
webContent = webContent.substring(start,end);
return webContent;
} catch (Exception e) {
e.printStackTrace();
return "error open url:" + strUrl;
}
}
}
JAVA如何获得外网IP地址?
java获取外网ip地址方法:
public class Main {
public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
EnumerationNetworkInterface netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() !finded) {
NetworkInterface ni = netInterfaces.nextElement();
EnumerationInetAddress address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
!ip.isLoopbackAddress()
ip.getHostAddress().indexOf(":") == -1) {// 内网IP
localip = ip.getHostAddress();
}
}
}
if (netip != null !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}
关于java获得公网ip和java web发布到公网的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。