InetAddress類:
public class NetTest {
/**設定緩存的屬性**/
public static final String PROPERTY_NETCACHE = "networkaddress.cache.ttl";
/**異常信息緩存屬性**/
public static final String PROPERTY_EXCEPTIONCACHE = "networkaddress.cache.negative.ttl";
public static void main(String[] args) throws IOException {
/*
* 初始化InetAddress host既可以是主機名也可以是ip地址 addr是以字節數組表示的ip地址
* 1:InetAddress.getByName(String host) 返回該主機所對應的第一個主機名/ip
* 2:InetAddress.getLocalHost() 返回本地主機 主機名/ip
* 3:InetAddress.getAllByName(String host) 返回該主機對應的所有 主機名/ip
* 4:InetAddress.getByAddress(byte[] addr) 返回 主機名/ip
* 5:InetAddress.getByAddress(String host, byte[] addr) 返回主機名/ip
*/
InetAddress ia = InetAddress.getByName("www.baidu.com");
out.println(ia);
InetAddress localIp = InetAddress.getLocalHost();
out.println(localIp);
InetAddress[] allIpAddress = InetAddress.getAllByName("www.google.com");
for (InetAddress address : allIpAddress){
System.out.println(address);
}
InetAddress byAddress = InetAddress.getByAddress(new byte[]{74,125,71,103});
out.println(byAddress);
InetAddress byHostAddress = InetAddress.getByAddress("www.baidu.com", new byte[]{119,75,218-256,45});
out.println(byHostAddress);
/*
* 設置緩存超時 單位秒 -1表示一直緩存
* 設置了緩存機制,第二次訪問該dns服務器就直接讀緩存
*/
Security.setProperty(PROPERTY_NETCACHE, "-1");
/*
* 設置異常信息緩存秒數
*/
Security.setProperty(PROPERTY_EXCEPTIONCACHE, "10");
//判斷能否連接
System.out.println(ia.isReachable(100));
/*
* 獲取ip字符串
*/
out.println(ia.getHostAddress());
/*
* 獲取該ip地址的主機別名 InetAddress由ip創建 返回主機名
*/
out.println(ia.getHostName());
/*
* 獲取該ip地址的主機名
*/
out.println(ia.getCanonicalHostName());
/*
* 獲取字節數組組成的ip地址
*/
out.println(ia.getAddress());
/*
* 使用ip創建的InetAddressgetHostName()才訪問服務器
*/
out.println(byAddress.getHostName());
/*
* Mire字符串的轉換
* URLDecoder 特殊---->一般 URLEncoder 一般---->特殊
*/
//解碼
String keyWord = URLDecoder.decode("%E8%8D%89%E6%B3%A5%E9%A9%AC", "UTF-8");
out.println(keyWord);
//編碼
String keyWordd = URLEncoder.encode(keyWord, "UTF-8");
out.println(keyWordd);
}
}