首先引入maven依赖
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
<version>2.1.8</version>
</dependency>
其中这个类是核心
InetAddress[] addresses= Address.getAllByName(host);
/**
* 通过dns解析域名得到IP列表
* @param host 域名
* @return
*/
public static List<String> getIPListByDNS(String host) throws UnknownHostException {
InetAddress[] addresses= Address.getAllByName(host);
List<String> ipList=new ArrayList<String>();
for (InetAddress inetAddress:addresses){
ipList.add(inetAddress.getHostAddress());
}
return ipList;
}