在Java中提供了專門的網(wǎng)絡(luò)開發(fā)程序包---java.net,java的網(wǎng)絡(luò)編程提供了兩種通信協(xié)議:TCP(傳輸控制協(xié)議)和UDP(數(shù)據(jù)報協(xié)議)。
一.ip(Internet PRotocol) 與InetAddress類
1.IP簡介
互聯(lián)網(wǎng)上的每一臺計算機都有一個唯一表示自己的標識,即IP地址。
IP地址=網(wǎng)絡(luò)地址+主機地址
2.InetAddress
該類主要表示IP地址,有兩個子類:Inet4Address、Inet6Address,前者表示IPV4,后者表示IPV6。
InetAddress類的常用方法有:
測試InetAddress類:
類型 方法 描述 static InetAddressgetByName(String host)在給定主機名的情況下確定主機的 IP 地址。 static InetAddress
getLocalHost()返回本地主機。 StringgetHostName()獲取此 IP 地址的主機名。 booleanisReachable(int timeout)測試是否可以達到該地址。 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(); } }}結(jié)果:
二.URL與URLConnection
1.URL
URL(Uniform Resource Locator)是統(tǒng)一資源定位符,可以直接使用此類找到互聯(lián)網(wǎng)上的資源(比如一個網(wǎng)頁)。
URL類常用方法:
類型 方法 描述 構(gòu)造方法
URL(String spec)根據(jù) String表示形式創(chuàng)建URL對象。構(gòu)造方法 URL(String protocol, String host, int port, String file)根據(jù)指定 protocol、host、port號和file創(chuàng)建URL對象。URLConnection openConnection()返回一個 URLConnection對象,它表示到URL所引用的遠程對象的連接。InputStream
openStream()打開到此 URL的連接并返回一個用于從該連接讀入的InputStream。
使用URL讀取內(nèi)容: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內(nèi)容 InputStream inputStream=url.openStream(); Scanner scan=new Scanner(inputStream); //設(shè)置讀取分隔符 scan.useDelimiter("/n"); while(scan.hasNext()){ //輸出內(nèi)容 System.out.println(scan.next()); } } catch (Exception e) { e.printStackTrace(); } }}結(jié)果:
顯示出來的是HTML代碼。
2.URLConnection
URLConnection是封裝訪問遠程網(wǎng)絡(luò)資源一般方法的類,通過它可以建立與遠程服務(wù)器的連接,檢查遠程資源的一些屬性。
常用方法:
類型 方法 描述 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("內(nèi)容大小:"+urlConn.getContentLength()); System.out.println("內(nèi)容類型:"+urlConn.getContentType()); } catch (Exception e) { e.printStackTrace(); } }}運行結(jié)果:
三.URLEncoder與URLDecoder
在java中如果需要完成編碼和解碼操作就要使用URLEncoder和URLDecoder兩個類。
URLEncoder類的方法:
類型 方法 描述 static Stringencode(String s, String enc)使用指定的編碼機制將字符串轉(zhuǎn)換為 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(); } }}運行結(jié)果:
新聞熱點
疑難解答