天天看点

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服务器