「java获得外网ip」java获取本机外网ip地址

博主:adminadmin 2022-12-04 18:33:07 69

今天给各位分享java获得外网ip的知识,其中也会对java获取本机外网ip地址进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何获取公网ip,有通过路由

如果要通过路由器,不同的路由器的获取方法不一样。通用的做法是通过 HttpClient 在百度上搜索关键字 ip, 然后提取出公网ip。

代码如下:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URL;

import java.net.URLConnection;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class App {

// 获取网页源码

static String httpGet(String url) {

StringBuffer buffer = new StringBuffer();

try {

URLConnection conn = new URL(url).openConnection();

conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36");

try (InputStream inputStream = conn.getInputStream();

InputStreamReader streamReader = new InputStreamReader(inputStream);

BufferedReader reader = new BufferedReader(streamReader);) {

String line = null;

while ((line = reader.readLine()) != null) {

buffer.append(line).append(System.lineSeparator());

}

}

} catch (IOException e) {

e.printStackTrace();

}

return buffer.toString();

}

public static void main(String[] args) {

String html = httpGet("");

// 提出IP

Pattern pattern = Pattern.compile("span\\sclass=\"c-gap-right\"本机IP:nbsp;([^]+)/span");

Matcher matcher = pattern.matcher(html);

if (matcher.find()) {

String ip = matcher.group(1);

System.out.println(ip);

}

}

}

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();

        }

    }

}

我在本机上,想写一个java程序获取我本机的外网ip地址,如何搞

import java.net.*;

public class Test {

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

String IP = null;

String host = null;

InetAddress ia;

ia = InetAddress.getLocalHost();

host = ia.getHostName();// 获取计算机名字

IP = ia.getHostAddress();// 获取IP

System.out.println(host);

System.out.println(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地址、java获得外网ip的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-04,除非注明,否则均为首码项目网原创文章,转载请注明出处。