1 3 package jodd.util; 4 5 import jodd.io.StreamUtil; 6 7 import java.io.IOException ; 8 import java.net.HttpURLConnection ; 9 import java.net.Inet4Address ; 10 import java.net.InetAddress ; 11 import java.net.URL ; 12 import java.net.UnknownHostException ; 13 14 17 public class NetUtil { 18 19 22 public static String resolveHost(String hostname) { 23 try { 24 InetAddress addr = Inet4Address.getByName(hostname); 25 byte[] ipAddr = addr.getAddress(); 26 StringBuffer ipAddrStr = new StringBuffer (15); 27 for (int i = 0; i < ipAddr.length; i++) { 28 if (i > 0) { 29 ipAddrStr.append('.'); 30 } 31 ipAddrStr.append(ipAddr[i] & 0xFF); 32 } 33 return ipAddrStr.toString(); 34 } catch (UnknownHostException uhex) { 35 return null; 36 } 37 } 38 39 42 public static String resolveIp(String ip) { 43 try { 44 InetAddress addr = InetAddress.getByName(ip); 45 return addr.getHostName(); 46 } catch (UnknownHostException uhex) { 47 return null; 48 } 49 } 50 51 54 public static String resolveIp(byte[] ip) { 55 try { 56 InetAddress addr = InetAddress.getByAddress(ip); 57 return addr.getHostName(); 58 } catch (UnknownHostException uhex) { 59 return null; 60 } 61 } 62 63 65 68 public static byte[] download(String url) throws IOException { 69 HttpURLConnection conection = (HttpURLConnection )((new URL (url).openConnection())); 70 return StreamUtil.readBytes(conection.getInputStream()); 71 } 72 } | Popular Tags |