Netty实战一 Netty实现文件的上传和下载,从头到尾,都是精华

所以下面这段代码的作用就是为这个子channel增加handle*/

.childHandler(pipeline);

ChannelFuture f = b.bind().sync();/异步绑定到服务器,sync()会阻塞直到完成/

System.out.println("Netty server start,port is " + port);

f.channel().closeFuture().sync();/阻塞直到服务器的channel关闭/

} finally {

group.shutdownGracefully().sync();/优雅关闭线程组/

}

}

}

使用netty实现服文件服务器端。

3、Pipeline

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.HttpRequestDecoder;

import io.netty.handler.codec.http.HttpResponseEncoder;

import io.netty.util.concurrent.DefaultEventExecutorGroup;

import io.netty.util.concurrent.EventExecutorGroup;

/**

  • 作者:DarkKIng

  • 创建日期:2019/12/17

  • 作用:职责链

*/

public class Pipeline extends ChannelInitializer {

private EventExecutorGroup businessEventExecutorGroup = new DefaultEventExecutorGroup(10);

@Override

protected void initChannel(SocketChannel ch) {

ChannelPipeline pipeline = ch.pipeline();

/**

  • http服务器端对response编码

*/

pipeline.addLast(“encoder”, new HttpResponseEncoder());

/**

  • http服务器端对request解码3.

*/

pipeline.addLast(“decoder”, new HttpRequestDecoder());

/**

  • 合并请求

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值