在Java中提供了專門的網絡開發程序包---java.net,java的網絡編程提供了兩種通信協議:TCP(傳輸控制協議)和UDP(數據報協議)。
一.IP(Internet Protocol) 與InetAddress類
1.IP簡介
互聯網上的每一台計算機都有一個唯一表示自己的標識,即IP地址。
IP地址=網絡地址+主機地址
2.InetAddress
該類主要表示IP地址,有兩個子類:Inet4Address、Inet6Address,前者表示IPV4,后者表示IPV6。
InetAddress類的常用方法有:
類型 方法 描述 static InetAddressgetByName(Stringhost)在給定主機名的情況下確定主機的 IP 地址。 static InetAddress
getLocalHost()返回本地主機。 StringgetHostName()獲取此 IP 地址的主機名。 booleanisReachable(int timeout)測試是否可以達到該地址。 測試InetAddress類:package org.demo.net; import java.net.InetAddress; /** * 測試InetAddress類 * @author oushine */ public class InetAddressDemo { public static void main(String[] args) { try { //聲明並得到本地InetAddress對象 InetAddress iAddress1=InetAddress.getLocalHost(); //聲明並得到遠程InetAddress對象 InetAddress iAddress2=InetAddress.getByName("www.baidu.com"); //獲得本地IP地址 System.out.println("本機IP地址為:"+iAddress1.getHostAddress()); //獲得遠程IP地址 System.out.println("百度的IP地址是:"+iAddress2.getHostAddress()); System.out.println("本機是否可達:"+iAddress1.isReachable(3000)); } catch (Exception e) { e.printStackTrace(); } } }結果:
二.URL與URLConnection
1.URL
URL(Uniform Resource Locator)是統一資源定位符,可以直接使用此類找到互聯網上的資源(比如一個網頁)。
URL類常用方法:
類型 方法 描述 構造方法
URL(String spec)根據 String表示形式創建URL對象。構造方法 URL(String protocol, String host, int port, String file)根據指定 protocol、host、port號和file創建URL對象。URLConnection openConnection()返回一個 URLConnection對象,它表示到URL所引用的遠程對象的連接。InputStream
openStream()打開到此 URL的連接並返回一個用於從該連接讀入的InputStream。
使用URL讀取內容:package org.demo.net; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Scanner; public class UrlDemo { public static void main(String[] args) { URL url; try { //指定操作的URL url = new URL("http","www.baidu.com",80,"/index.html"); //打開輸入流,讀取URL內容 InputStream inputStream=url.openStream(); Scanner scan=new Scanner(inputStream); //設置讀取分隔符 scan.useDelimiter("\n"); while(scan.hasNext()){ //輸出內容 System.out.println(scan.next()); } } catch (Exception e) { e.printStackTrace(); } } }
結果:
顯示出來的是HTML代碼。
2.URLConnection
URLConnection是封裝訪問遠程網絡資源一般方法的類,通過它可以建立與遠程服務器的連接,檢查遠程資源的一些屬性。
常用方法:
類型 方法 描述 int
getContentLength()返回 content-length頭字段的值。String
getContentType()返回 content-type頭字段的值。InputStream
getInputStream()返回從此打開的連接讀取的輸入流。 URLConnection對象可以通過URL類的openConnection()方法取得。
取得URL的基本信息:package org.demo.net; import java.net.URL; import java.net.URLConnection; public class URLConnectionDemo { public static void main(String[] args) { try { URL url=new URL("http://www.baidu.com"); //建立連接 URLConnection urlConn=url.openConnection(); System.out.println("內容大小:"+urlConn.getContentLength()); System.out.println("內容類型:"+urlConn.getContentType()); } catch (Exception e) { e.printStackTrace(); } } }運行結果:
三.URLEncoder與URLDecoder
在java中如果需要完成編碼和解碼操作就要使用URLEncoder和URLDecoder兩個類。
URLEncoder類的方法:
類型 方法 描述 static Stringencode(String s, String enc)使用指定的編碼機制將字符串轉換為 application/x-www-form-urlencoded格式。
URLDecoder類的方法:
類型 方法 描述 static Stringdecode(String s, String enc)使用指定的編碼機制對 application/x-www-form-urlencoded字符串解碼。編碼及解碼操作:package org.demo.net; import java.net.URLDecoder; import java.net.URLEncoder; public class CodeDemo { public static void main(String[] args) { String keyWord="oushine 陽"; try { String enCode=URLEncoder.encode(keyWord, "UTF-8"); System.out.println("編碼之后:"+enCode); String deCode=URLDecoder.decode(enCode, "UTF-8"); System.out.println("解碼之后:"+deCode); } catch (Exception e) { e.printStackTrace(); } } }運行結果:
轉自:https://www.cnblogs.com/yzl-i/p/4442892.html#top


