zoukankan      html  css  js  c++  java
  • Netty实现http

    public class HttpServer2 {
        public static void main(String[] args) throws Exception {
            new HttpServer2(8084).start();
        }
    
        int port;
    
        public HttpServer2(int port) {
            this.port = port;
        }
    
        public void start() throws Exception {
            ServerBootstrap bootstrap = new ServerBootstrap();
            EventLoopGroup boss = new NioEventLoopGroup();
            EventLoopGroup work = new NioEventLoopGroup();
            bootstrap.group(boss, work)
                    //.handler(new LoggingHandler(LogLevel.DEBUG))
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
    //                        pipeline.addLast(new HttpServerCodec());// http 编解码
                            pipeline.addLast(new HttpRequestDecoder());// http 编解码
                            pipeline.addLast(new HttpResponseEncoder());
    //                        pipeline.addLast("httpAggregator", new HttpObjectAggregator(512 * 1024)); // http 消息聚合器   512*1024为接收的最大contentlength
    //                        pipeline.addLast(new HttpRequestHandler());// 请求处理器
                            pipeline.addLast(new MyRequestHandler());
                        }
                    });
    
            ChannelFuture f = bootstrap.bind(new InetSocketAddress(port)).sync();
            System.out.println(" server start up on port : " + port);
            f.channel().closeFuture().sync();
    
        }
    
    }
    public class MyRequestHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    
            if (!(msg instanceof DefaultHttpRequest)) {
                return;
            }
            String uri = "/a";//req.uri();
    
            String msg1 = "<html><head><title>test</title></head><body>你请求uri为:" + uri + "</body></html>";
            // 创建http响应
            FullHttpResponse response = new DefaultFullHttpResponse(
                    HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    Unpooled.copiedBuffer(msg1, CharsetUtil.UTF_8));
            // 设置头信息
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, msg1.getBytes().length);
    
            ctx.writeAndFlush(response);//.addListener(ChannelFutureListener.CLOSE);
        }
    }
  • 相关阅读:
    zstu.4189: 逻辑运算(构建 && 前缀表达式入门)
    Oulipo (kmp)
    zstu.4194: 字符串匹配(kmp入门题&& 心得)
    杭赛.Triangle(数学推导)
    cf.VK CUP 2015.C.Name Quest(贪心)
    微服务技术栈
    luogu3320 寻宝游戏 (dfs序+倍增lca+set)
    cf757F Team Rocket Rises Again (dijkstra+支配树)
    cf983E NN Country (倍增+dfs序+树状数组)
    cf860E Arkady and A Nobody-men (树剖)
  • 原文地址:https://www.cnblogs.com/Tony100/p/14205705.html
Copyright © 2011-2022 走看看