標籤:java url socket
URL:
URL是統一資源定位器(Uniform Resource Locator)的簡稱.它表示互連網上某一資源的地址.瀏覽器通過給定的URL可以找到相應的檔案或其他資源.在某些情況下,URL中除IP地址以外的部分可以省略.例如:在瀏覽器地址欄輸入java.sun.com,瀏覽器會預設使用http協議及相應的連接埠號碼,並使用Web伺服器提供的預設的檔案.URL的一般例子,如: http://java.sum.com , ftp:192.168.10.233 及http://java.sum.com/javase/downloads/index.jsp.
在java中,使用java.net中的URL類可以建立代表互連網上某一具體資源的URL對象.通過此對象,利用相關的方法,可以輕鬆的進行網路資源的存取.
2.1 URL類
java.net 包提供URL類,用URL對象表示URL地址.
(1) URL類的構造方法
URL類提供多種不同的構造方法,用於以不同形式建立URL對象.
public URL(String spec) throws MalformedURLException
public URL(URL context, String spec) throws MalformedURLException
public URL(String protocol, String host, String file) throws MalformedURLException
public URL(String protocol, String host, int port, String file) throws MalformedURLException
其中,參數spec是由協議名,主機名稱,連接埠號碼,檔案名稱組成的字串.參數context是已建立的URL對象,參數protocol是協議名,參數host是主機名稱,參數file是檔案名稱,對數port是連接埠號碼.
下面通過各種構造方法建立URL對象,分別以不同的方式提供URL地址的各部分資訊
URL myURL1=new URL(“http://www.tju.edu.cn:80/”);
URL myURL2=new URL(“myURL1”,”support/fap.html”);
URL myURL3=new URL(“http”,”www.tju.edu.cn”,”index.html”);
URL myURL4=new URL(“http”,”www.tju.edu.cn”,80,”index.html”);
上面的myURL2地址是由myURL1地址和用相對路徑表示的檔案名稱會成的,代表的URL地址是:http://www.tju.edu.cn:80/support/fap.html
(2)擷取URL對象的屬性
一個URL對象中包括各種屬性,屬性不能被改變,但可以通過下面的方法擷取屬性
public String getProtocol() //擷取URL的協議名
public String getHost() //擷取URL 的主機名稱
public int getPort() //擷取URL的連接埠號碼
public String getPath() //擷取URL的檔案路徑
public String getFile() //擷取URL的檔案名稱
public String getRef() //擷取URL在檔案中的相對位置
public String getQuery() //擷取URL的查詢名
2.2 利用URL訪問網上資源
一個URL對象對應一個網址,,產生URL對象後,就可以調用URL對象的openStream()方法讀取網址中的資訊.openStream()方法的原型如下:
public final InputSream openStream()
調用openStream()方法擷取的是一個InputSream輸入資料流對象,通過read()方法只能從這個輸入資料流中逐位元組讀取資料,也就是從URL網址中逐位元組讀取資訊,為了能方便地從URL讀取資訊,通常將原始的InputSream輸入資料流轉變為其他類型的輸入資料流,如BufferedReader等,
執行個體1:
import java.io.*;import java.net.*;public class URL1 {public static void main(String[] args) {// TODO Auto-generated method stubtry{URL u1=new URL("http://www.qq.com/");System.out.println(u1.getProtocol());System.out.println(u1.getHost());System.out.println(u1.getPort());System.out.println(u1.getFile());}catch(MalformedURLException e){System.out.println(e);}catch(IOException ee){System.out.println(ee);}catch(Exception eee){System.out.println(eee);}}}
執行個體2:
import java.io.*;import java.net.*;public class URLT {public static void main(String[] args) {// TODO Auto-generated method stubtry{URL u1=new URL("http://www.hao123.com/");BufferedReader br=new BufferedReader(new InputStreamReader(u1.openStream()));String s;while((s=br.readLine())!=null){System.out.println(s);}br.close();}catch(MalformedURLException e){System.out.println(e);}catch(IOException ee){System.out.println(ee);}catch(Exception eee){System.out.println(eee);}}}
Socket:
客戶:
import java.io.*;import java.net.*;public class Kehu {public static void main(String[] args) {// TODO Auto-generated method stubtry{Socket s=new Socket("127.0.0.1", 2007);InputStream is=s.getInputStream();OutputStream os=s.getOutputStream();PrintStream ps=new PrintStream(os);ps.println("hello");DataInputStream dis=new DataInputStream(is);String request=dis.readLine();System.out.println(request);s.close();}catch(ConnectException e){System.out.println(e);}catch(IOException ee){System.out.println(ee);}catch(Exception eee){System.out.println(eee);}}}
服務:
import java.net.*;import java.io.*;public class Fuwu {public static void main(String[] args) {// TODO Auto-generated method stubtry{ServerSocket ss=new ServerSocket(2007);while(true){Socket s=ss.accept();InputStream is=s.getInputStream();OutputStream os=s.getOutputStream();DataInputStream dis=new DataInputStream(is);String request=dis.readLine();PrintStream ps=new PrintStream(os);ps.println("good bye");s.close();ss.close();}}catch(IOException e){System.out.println(e);}catch(Exception ee){System.out.println(ee);}}}
Java 學習 ——網路與通訊