天天看點

Netty實作簡單HTTP伺服器

netty

主類:

測試:

用浏覽器通路:http://localhost:8080/

Netty實作簡單HTTP伺服器

1、首先加入的是http請求消息解碼器

ch.pipeline().addlast("http-decoder", new httprequestdecoder());

2、第2添加httpobjectaggregator解密器,其作用是将多個消息轉換為單一的fullhttprequest或者fullhttpresponse,原因是http解碼器在每個http消息中會生成多個消息對象:有1、httprequest/httpresponse;2、httpcontent;3、lasthttpcontent;

ch.pipeline().addlast("http-aggregator", new httpobjectaggregator(65536));

3、第3增加http響應編碼器,對http響應資訊進行編碼

ch.pipeline().addlast("http-encoder", new httpresponseencoder());

4、第4chunked handler的主要作用是支援異步發送大的碼流(例如大的檔案傳輸),但不占用過多的記憶體,防止發生java記憶體溢出錯誤

ch.pipeline().addlast("http-chunked", new chunkedwritehandler());

5、第5httpfileserverhandler用于檔案伺服器的業務邏輯處理

檔案讀取下載下傳

示範:

Netty實作簡單HTTP伺服器