IP 域名转换 IP版本判断
IP 域名转换
/** * 将域名转换成IP,若返回null,则判断失败 * @param host * @return */ public String GetInetAddress(String host) { String IPAddress = ""; InetAddress ReturnStr1 = null; try { ReturnStr1 = java.net.InetAddress.getByName(host); IPAddress = ReturnStr1.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); return IPAddress; } return IPAddress; }
IP版本判断
/** * 判断IP版本是IPv4还是IPv6,若返回null,则判断失败 * @param host * @return */ public String GetIPVersion(String host) { InetAddress ReturnStr1 = null; try { ReturnStr1 = java.net.InetAddress.getByName(host); if (ReturnStr1.getAddress().length == 4) { return "ipv4"; } else { return "ipv6"; } } catch (UnknownHostException e) { e.printStackTrace(); } return null; }
疑问:
IP能否转成额域名??是否有必要??