适合初中级Java程序员修炼手册从0搭建整个Web项目(一)( 二 )

复制代码HttpServerNetty 编写 HTTP 服务器主类
package com.xiaoliuliu.six.finger.web.server;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.logging.LogLevel;import io.netty.handler.logging.LoggingHandler;import java.net.InetSocketAddress;/** * @author 小六六 * @version 1.0 * @date 2020/10/13 11:41 * Netty 编写 HTTP 服务器 * 主类 */public class HttpServer {/*** @Des 端口 http请求的端口* @Author 小六六* @Date 2020/10/13 11:42* @Param* @Return*/int port;/*** @Des 构造方法* @Author 小六六* @Date 2020/10/13 11:42* @Param* @Return*/public HttpServer(int port) {this.port = port;}/*** @Des 服务的启动方法* @Author 小六六* @Date 2020/10/13 11:43* @Param* @Return*/public void start() throws Exception {//启动引导类ServerBootstrap bootstrap = new ServerBootstrap();NioEventLoopGroup boss = new NioEventLoopGroup();NioEventLoopGroup work = new NioEventLoopGroup();bootstrap.group(boss, work).handler(new LoggingHandler(LogLevel.DEBUG)).channel(NioServerSocketChannel.class).childHandler(new HttpServerInitializer());ChannelFuture cf = bootstrap.bind(new InetSocketAddress(port)).sync();System.out.println(" server start up on port : " + port);cf.channel().closeFuture().sync();}}复制代码HttpServerInitializerpackage com.xiaoliuliu.six.finger.web.server;import io.netty.channel.ChannelHandler;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.socket.SocketChannel;import io.netty.handler.codec.http.HttpObjectAggregator;import io.netty.handler.codec.http.HttpServerCodec;/** * @author 小六六 * @version 1.0 * @date 2020/10/13 11:57 * 用于配置 pipeline的处理链 */public class HttpServerInitializer extends ChannelInitializer {@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline = socketChannel.pipeline();// http 编解码pipeline.addLast(new HttpServerCodec());// http 消息聚合器pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024));// 请求处理器pipeline.addLast(new HttpRequestHandler());}}复制代码HttpRequestHandlerpackage com.xiaoliuliu.six.finger.web.server;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandler;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.handler.codec.http.*;import io.netty.util.AsciiString;import io.netty.util.CharsetUtil;import java.util.HashMap;import java.util.Map;import static io.netty.handler.codec.http.HttpUtil.is100ContinueExpected;/** * @author 小六六 * @version 1.0 * @date 2020/10/13 12:01 * 核心处理http请求的类 , 包括url的匹配核心方法都是在channelRead0方法 */public class HttpRequestHandler extends SimpleChannelInboundHandler {private static final String FAVICON_ICO = "/favicon.ico";private static final AsciiString CONNECTION = AsciiString.cached("Connection");private static final AsciiString KEEP_ALIVE = AsciiString.cached("keep-alive");@Overridepublic void channelReadComplete(ChannelHandlerContext ctx) {ctx.flush();}@Overrideprotected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {System.out.println("获得的参数:"+req);if (is100ContinueExpected(req)) {ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.CONTINUE));}// 获取请求的uriString uri = req.uri();Map resMap = new HashMap


推荐阅读