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

猜你喜欢

转载自blog.csdn.net/lhb2019/article/details/79858884