zoukankan      html  css  js  c++  java
  • unsupported message type: DefaultFullHttpResponse (expected: ByteBuf, FileRegion) 原因以及解决办法

    使用netty做http服务器的时候 用android链接 会出现这个错误

    原因是http-aggregator顺序有问题

    (ps:目前大部分国内博客都是这个排序有点坑爹):

    官方文档说明:For convenience, consider putting a HttpServerCodec before the HttpObjectAggregator as it functions as both a HttpRequestDecoder and a HttpResponseEncoder.

    package com.sencorsta.ids.httptest;

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.codec.http.HttpObjectAggregator;
    import io.netty.handler.codec.http.HttpRequestDecoder;
    import io.netty.handler.codec.http.HttpResponseEncoder;
    import io.netty.handler.stream.ChunkedWriteHandler;

    /**
    * Created by apple on 17/10/21.
    */
    public class HttpServer {

    public static void start(final int port) throws Exception {
    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup woker = new NioEventLoopGroup();
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    HttpServerHandler httpServerHandler=new HttpServerHandler();
    try {

    serverBootstrap.channel(NioServerSocketChannel.class).group(boss, woker)
    .childOption(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_BACKLOG, 1024)
    .childHandler(new ChannelInitializer<SocketChannel>() {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
    ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));//这里的位置有问题
    ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
    ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
    ch.pipeline().addLast(httpServerHandler);
    }
    });

    ChannelFuture future = serverBootstrap.bind(port).sync();
    future.channel().closeFuture().sync();
    } finally {
    boss.shutdownGracefully();
    woker.shutdownGracefully();
    }
    }

    public static void main(String[] args) throws Exception {
    start(8080);
    }
    }
    把上面的ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));这一行

    放到coder下面就可以了

    package com.sencorsta.ids.httptest;

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.codec.http.HttpObjectAggregator;
    import io.netty.handler.codec.http.HttpRequestDecoder;
    import io.netty.handler.codec.http.HttpResponseEncoder;
    import io.netty.handler.stream.ChunkedWriteHandler;

    /**
    * Created by apple on 17/10/21.
    */
    public class HttpServer {

    public static void start(final int port) throws Exception {
    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup woker = new NioEventLoopGroup();
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    HttpServerHandler httpServerHandler=new HttpServerHandler();
    try {

    serverBootstrap.channel(NioServerSocketChannel.class).group(boss, woker)
    .childOption(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_BACKLOG, 1024)
    .childHandler(new ChannelInitializer<SocketChannel>() {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
    ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
    ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
    ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
    ch.pipeline().addLast(httpServerHandler);
    }
    });

    ChannelFuture future = serverBootstrap.bind(port).sync();
    future.channel().closeFuture().sync();
    } finally {
    boss.shutdownGracefully();
    woker.shutdownGracefully();
    }
    }

    public static void main(String[] args) throws Exception {
    start(8080);
    }
    }
    参考资料:官方说明

  • 相关阅读:
    hyperV 虚拟机的创建和内存管理
    诡异~~ASP.NET 程序 无法上传文件 (这个..自己大意造成的嘛~~)
    多线程扫描,多线程采集, ftpscan.NET
    ASP.NET 调试
    Hyperv 如何使用WMI向虚拟计算机附加硬盘
    VS2008SP1显示中文版的智能感知提示信息
    NGIX
    python3的linux环境编译安装
    NGIX之项目布署
    android之自定义ViewGroup和自动换行的布局的实现(支持按钮间隔)
  • 原文地址:https://www.cnblogs.com/exmyth/p/15050386.html
Copyright © 2011-2022 走看看