java 协议处理器_协议处理器urlstreamhandler及contenthandler
先看段打開網頁的代碼:URL?url=new?URL("http://souljava.blog.163.com/");
URLConnection?connection=url.openConnection();
connection.getInputStream();
問題1:客戶端瀏覽器怎么判斷接受到的是什么數據類型?
回答:java的附帶瀏覽器JEditorPane會按以下方式,依次判斷
URLConnectiongetContentType()
根據?URL?的指定?"file"?部分嘗試確定對象的內容類型。
根據輸入流的開始字符嘗試確定輸入流的類型。
問題2:如果傳送的數據類型不在(RFC)?中怎么辦?
回答:自定義協議處理框架
貼幾個關鍵的類:
*abstract?classURLConnection
表示客戶程序與遠程服務器的連接。?客戶程序可以從URLConnection獲得輸入輸出流
推薦覆蓋:
StringgetContentType()從URL?判斷返回何種MIME
abstract??voidconnect()解析URL?,由自己實現的超類建立soket連接
getInputStream()從soket連接中,獲取輸入流
*public?abstract?classURLStreamHandler
協議處理器,主要負責創建與協議相關的URLConnection對象.
推薦覆蓋:
protected?abstractopenConnection(URLu)
URLStreamHandler?對象被重載URLStreamHandlerFactory的createURLStreamHandler(Stringprotocol)
*abstract?classContentHandler
內容處理器,負責解析服務器發送的數據,然后轉換成?合適的java?數據結構
推薦覆蓋:
依次遍歷classes中的元素如果匹配就返回
ContentHandler?由ContentHandlerFactory?創建
需要覆蓋:createContentHandler(Stringmimetype)?判斷產生何種ContentHandler
自定義協議處理框架
自定義的echo協議import?java.net.*;
import?java.io.*;
import?echo.*;
public?class?EchoClient{
public?static?void?main(String?args[])throws?IOException{
//設置URLStreamHandlerFactory
URL.setURLStreamHandlerFactory(new?EchoURLStreamHandlerFactory());
//設置ContentHandlerFactory
URLConnection.setContentHandlerFactory(new?EchoContentHandlerFactory());
URL?url=new?URL("echo://localhost:8000");
EchoURLConnection?connection=(EchoURLConnection)url.openConnection();
connection.setDoOutput(true);
PrintWriter?pw=new?PrintWriter(connection.getOutputStream(),true);
while(true){
BufferedReader?br=new?BufferedReader(new?InputStreamReader(System.in));
String?msg=br.readLine();
pw.println(msg);??//向服務器發送消息
String?echoMsg=(String)connection.getContent();?//讀取服務器返回的消息
System.out.println(echoMsg);
if(echoMsg.equals("echo:bye")){
connection.disconnect();?//斷開連接
break;
}
}
}
}
package?echo;
import?java.net.*;
import?java.io.*;
public?class?EchoContentHandler?extends?ContentHandler{
public?Object?getContent(URLConnection?connection)throws?IOException{
InputStream?in=connection.getInputStream();
BufferedReader?br=new?BufferedReader(new?InputStreamReader(in));
return?br.readLine();
}
public?Object?getContent(URLConnection?connection,Class[]?classes)throws?IOException{
InputStream?in=connection.getInputStream();
for(int?i=0;i
if(classes[i]==InputStream.class)
return?in;
else?if(classes[i]==String.class)
return?getContent(connection);
}
return?null;
}
}
package?echo;
import?java.net.*;
public?class?EchoContentHandlerFactory?implements?ContentHandlerFactory{
public?ContentHandler?createContentHandler(String?mimetype){
if(mimetype.equals("text/plain")){
return?new?EchoContentHandler();
}else{
return?null;
}
}
}
package?echo;
import?java.net.*;
import?java.io.*;
public?class?EchoURLConnection?extends?URLConnection{
private?Socket?connection=null;
public?final?static?int?DEFAULT_PORT=8000;
public?EchoURLConnection(URL?url){
super(url);
}
public?synchronized?InputStream?getInputStream()?throws?IOException{
if(!connected)connect();
return?connection.getInputStream();
}
public?synchronized?OutputStream?getOutputStream()?throws?IOException{
if(!connected)connect();
return?connection.getOutputStream();
}
public?String?getContentType(){
return?"text/plain";
}
public?synchronized?void?connect()throws?IOException{
if(!connected){
int?port=url.getPort();
if(port<0?||?port>65535)port=DEFAULT_PORT;
this.connection=new?Socket(url.getHost(),port);
this.connected=true;
}
}
public?synchronized?void?disconnect()?throws?IOException{
if(connected){
this.connection.close();
this.connected=false;
}
}
}
package?echo;
import?java.net.*;
import?java.io.*;
public?class?EchoURLStreamHandlerFactory?implements?URLStreamHandlerFactory{
public?URLStreamHandler?createURLStreamHandler(String?protocol){
if(protocol.equals("echo"))
return?new?EchoURLStreamHandler();
else
return?null;
}
}
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java 协议处理器_协议处理器urlstreamhandler及contenthandler的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle课程设计摘要,Oracle程
- 下一篇: 计算机插件技术应用原理,计算机软件技术中