netty 实现一个断点续传下载工具~支持http,websocket协议~

/ 后端 / 没有评论 / 633浏览

支持http断点续传下载,后续支持磁力bt等;后端使用netty框架编写,支持http协议接口认证及调用,支持websocket协议对前端进行主动推送下载进度等信息;

1.项目结构:

分别为公共模块,核心模块还有web端显示仪表模块;

2.从main方法入口类来看:

 public static void main(String[] args) {
        try {
            //添加主线程关闭钩子,做一些提示等
            addShutdownHook();
        <span class="hljs-comment">//读取用户配置文件</span>
        config = ConfigInit.readConfig();
    
        <span class="hljs-comment">//加载日志框架</span>
        LogInit.loadLog(config);
        
        <span class="hljs-comment">//启动netty服务器</span>
        ThreadPoolUtil.POOL.getPool().execute(() -&gt; {
            <span class="hljs-keyword">try</span> {
                NettyServer.getInstance().start(config);
            } <span class="hljs-keyword">catch</span> (Exception e) {
                e.printStackTrace();
            } <span class="hljs-keyword">finally</span> {
                NettyServer.getInstance().stop();
            }
        });
        
        <span class="hljs-comment">//输出启动信息</span>
        systemOutStart();
    } <span class="hljs-keyword">catch</span> (Exception e) {
        e.printStackTrace();
        System.exit(-<span class="hljs-number">1</span>);
    }
}

3.netty服务器配置:

 private static void addHandler(ChannelPipeline pipeline) {
        //http编解码
        pipeline.addLast(new HttpServerCodec());
        //将大型文件从文件系统复制到内存进行块传输
        pipeline.addLast(new ChunkedWriteHandler());
        //支持post包文件
        pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
        //解析为websocket协议
        pipeline.addLast(new HttpServerExpectContinueHandler());
        //检测连接心跳
        pipeline.addLast(new IdleStateHandler(60, 60 * 60, 60 * 60));
        //websocket登陆拦截
        pipeline.addLast(new WebsocketLoginHandler());
        //websocket拦截路径及发送文件控制
        pipeline.addLast(new WebSocketServerProtocolHandler("/gyws", null, true, 65536 * 10));
        //websocket核心处理类
        pipeline.addLast(new WebSocketHandler());
        //http服务登陆拦截
        pipeline.addLast(new HttpLoginHandler());
        //http服务转换格式
        pipeline.addLast(new HttpJsonConventHandler());
        //http核心处理类
        pipeline.addLast(new HttpServerHandler());
        //尾巴处理类
        pipeline.addLast(new TailHandler());
    }

4.http接收下载服务后大致步骤:

1.生成下载文件任务状态文件job.json;

2.检测该下载是否正常;

3.写入文件到临时文件,并生成该文件独有配置文件,支持断点续传;

4.下载完毕进行合并删除临时文件;

5.实时计算下载速率与进度;

关注点:全局状态文件的修改使用到了随机文件锁避免并发修改;任务的销毁等主要线程的安全销毁动作;

5.websocket功能:

1.监测所有文件的下载,暂停,删除等命令,进行实时推送到web端;

关注点: 以下代码如果控制websocket的连接数,会出现问题,这里会把http连接也会实时加进来,因为websocket也是http协议的一种;所以换种思路可以在接收websocket的ping信息时进行统计阻止;

public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        /**
        *   if(count>2){
        *      断开连接    
        *  }
        */
        GyCache.channelList.add(ctx);
    }

6.还没搞:

1.没有完善登陆页面,目前只是使用固定token进行登陆;

2.目前只是功能完成,需要进一步重构优化代码;

github : https://github.com/785175323/GY-Download 

喜欢点星星