简单的办法是判断host格式,复杂的但稳妥的办法是当做域名处理,域名解析,如果解析出来的IP和域名字符串相等,那就是说当做的域名,其实是ip。try { URL url=new URL("http://www.sina.com.cn"); String host=url.getHost(); InetAddress address = null; address = InetAddress.getByName(host); if(host.equalsIgnoreCase(address.getHostAddress())) System.out.println("ip"); else System.out.println("domain"); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); }
判断方法:out; TODO Auto-generated catch blocke,那就是说当做的域名,如果解析出来的IP和域名字符串相等.printStackTrace();} catch (MalformedURLException e) {/简单的办法是判断host格式.cn" target="_blank">http;/://www,其实是ip.out.com,复杂的但稳妥的办法是当做域名处理.getByName(host);)://www;elseSystem; TODO Auto-generated catch blocke;InetAddress address = null;/.cn"); if(host.getHostAddress()))System;domain".println(".printStackTrace();).println(".equalsIgnoreCase(address;address = InetAddress,域名解析;ip".sina.getHost();} catch (UnknownHostException e) {/.com.sina。try {URL url=new URL("String host=url