標籤:inetaddres url 執行個體
InetAddress類
/* * InetAddress類 */public class Test07 { public static void main(String[] args) throws UnknownHostException { //擷取原生InetAddress執行個體 System.out.println("*******擷取原生InetAddress執行個體**********"); InetAddress address1 = InetAddress.getLocalHost(); System.out.println("主機名稱:"+address1.getHostName()); System.out.println("IP地址:"+address1.getHostAddress()); byte[] bytes=address1.getAddress(); System.out.println("位元組數組形式的IP地址:"+Arrays.toString(bytes));//超過127則為負數,加256 System.out.println(address1); //根據主機名稱擷取指定主機的InetAddress執行個體 System.out.println("*******根據主機名稱擷取指定主機的InetAddress執行個體**********"); //InetAddress address2=InetAddress.getByName("HE67B6DJBOID2DA"); InetAddress address2=InetAddress.getByName("5.8.6.45"); System.out.println(address2.getHostName()); System.out.println(address2); //根據位元組數組形式的IP地址來擷取InetAddress執行個體 System.out.println("**********根據位元組數組形式的IP地址來擷取InetAddress執行個體***********"); byte[] bytes2={(byte)192,(byte)168,6,8}; InetAddress address3=InetAddress.getByAddress(bytes2); System.out.println(address3.getHostName()); System.out.println(address3); }}
URL常用方法
/* * URL常用方法 */public class Test08 { public static void main(String[] args) throws MalformedURLException { URL baidu=new URL("http://www.baidu.com"); URL url=new URL(baidu, "/index.html?username=tom&password=123#test"); System.out.println("協議:"+url.getProtocol()); System.out.println("主機:"+url.getHost()); System.out.println("連接埠:"+url.getPort()); System.out.println("檔案路徑:"+url.getPath()); System.out.println("檔案名稱:"+url.getFile()); System.out.println("相對路徑:"+url.getRef()); System.out.println("查詢:"+url.getQuery()); }}
使用URL讀取網頁內容
import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;/* * 使用URL讀取網頁內容 */public class Test01 { public static void main(String[] args) throws IOException { test02(); } //讀取網頁資源 public static void test01() throws IOException{ URL url = new URL("http://localhost:8080/webTest/"); InputStream is = url.openStream();// 擷取URL對象所表示的資源的輸入資料流 BufferedReader br = new BufferedReader(new InputStreamReader(is));// 將位元組輸入資料流轉換為字元輸入資料流 String str = br.readLine(); while (str != null) { System.out.println(str); str = br.readLine(); } br.close(); is.close(); } //讀取FTP上的檔案資源 public static void test02() throws IOException{ URL url=new URL("ftp://wbs14061:[email protected]/資料/exe2.txt"); System.out.println("協議:"+url.getProtocol()); System.out.println("主機:"+url.getHost()); System.out.println("連接埠:"+url.getPort()); System.out.println("檔案路徑:"+url.getPath()); System.out.println("檔案名稱:"+url.getFile()); System.out.println("相對路徑:"+url.getRef()); System.out.println("查詢:"+url.getQuery()); InputStream is = url.openStream();// 擷取URL對象所表示的資源的輸入資料流 BufferedReader br = new BufferedReader(new InputStreamReader(is));// 將位元組輸入資料流轉換為字元輸入資料流 String str = br.readLine(); while (str != null) { System.out.println(str); str = br.readLine(); } br.close(); is.close(); }}
JAVA學習筆記(五十八)- InetAddress類與URL