/**
* 根据域名获取主机的IP
* @param hostName 域名
*/
public static String getIPByName(String hostName) {
String hostAddr = new String();
try {
InetAddress addr = InetAddress.getByName(hostName);
hostAddr = addr.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostAddr;
* 根据域名获取主机的IP
* @param hostName 域名
*/
public static String getIPByName(String hostName) {
String hostAddr = new String();
try {
InetAddress addr = InetAddress.getByName(hostName);
hostAddr = addr.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostAddr;
}
/**
* 根据域名获得主机所有的IP地址.
* 输入空,则会返回127.0.0.1
* @param hostName 域名
*/
public static List<String> getAllIPByName(String hostName) {
List<String> ips = new ArrayList<String>();
try {
InetAddress[] addrs = InetAddress.getAllByName(hostName);
for (int i = 0; i < addrs.length; ++i) {
ips.add(addrs[i].getHostAddress());
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ips;
}