我是靠谱客的博主 兴奋画笔,这篇文章主要介绍java获取域名ip,现在分享给大家,希望可以做个参考。

需要的包Jsoup1.6.jar

package www.xuexibc.com.util;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

public class GetIp {
    public static List GetIp(String url){
        List list=new ArrayList<>();
        try {
            URL uri=new URL(url);
            String host=uri.getHost();//获取域名
            System.out.println(host);
            try {
                InetAddress[] addresslist=InetAddress.getAllByName(host);//获取ip数组
                for(InetAddress a:addresslist){//遍历
                    list.add(a.getHostAddress());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } 
        return list;
    }
    public static void main(String[] args) {
        List list=GetIp.GetIp("http://www.xuexibc.top/");
        System.out.println(list);
    }
}

注:http://www.xuexibc.top/是我在阿里云买来实验的,运行程序获得了内网和外网ip

最后

以上就是兴奋画笔最近收集整理的关于java获取域名ip的全部内容,更多相关java获取域名ip内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部