美文网首页
ftp多线程上传、下载以及断点续传

ftp多线程上传、下载以及断点续传

作者: 程序员ken | 来源:发表于2021-06-26 20:49 被阅读0次

    上传功能

    首先加入默认的配置项(这部分有对应的方法进行设置):

    file

    异步上传的部分代码

    
            /// <summary>
            /// 异步上传 (多个文件最好用这个方法)    多线程时设置最大连接数 可以保证按队列先入先出去传输 此时不需要设置线程池中线程数量
            /// </summary>
            /// <param name="fullName"></param>
            /// <param name="fileName">如果为空 默认取上传文件的名称</param>
            /// <param name="process"></param>
            public  void AsynUpload(string fullName,string fileName, Func<FtpState, FtpState> process)
            {
                ManualResetEvent waitObject;
                FtpState state = new FtpState();
                try
                {
                    string _port = string.IsNullOrEmpty(port) ? "" : $":{port}";
                    fileName = string.IsNullOrEmpty(fileName) ? Path.GetFileName(fullName) : fileName;
                    string ftpfullpath = $"ftp://{ipAddr}{_port }//{fileName}"; 
                    Uri target = new Uri(ftpfullpath);
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
                    request.Method = WebRequestMethods.Ftp.UploadFile;
                    request.Credentials = new NetworkCredential(account, password);
                    //request.KeepAlive = false;//是否保存连接  设置了这个并不能生效  异步上传
                    request.UseBinary = true;// ftp默认是传输二进制
                    request.ServicePoint.ConnectionLimit = maxConnect;//最大连接数
    
                    // Store the request in the object that we pass into the
                    // asynchronous operations.
                    state.Request = request;
                    state.FullName = fullName;
                    state.ProcessCall = process;
                    state.Operate = FtpOperate.UpLoad;
    
                    //文件大小
                    FileInfo file = new FileInfo(fullName);
                    state.Size = file.Length;
    
                    // Get the event to wait on.
                    waitObject = state.OperationComplete;
    
                    // Asynchronously get the stream for the file contents.
                    request.BeginGetRequestStream(
                        new AsyncCallback(EndGetStreamCallback),
                        state
                    );
    
                    // Block the current thread until all operations are complete.
                    waitObject.WaitOne();
    
                    // The operations either completed or threw an exception.
                    if (state.OperationException != null)
                    {
                        if (UploadFail != null)
                        {
                            UploadFail.Invoke(state, state.OperationException);
                            return;
                        }
                        throw state.OperationException;
                    }
                    else
                    {
                        if (UploadSuccess != null)
                        {
                            UploadSuccess.Invoke(state, state.StatusDescription);
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (UploadFail != null)
                    {
                        UploadFail.Invoke(state, ex);
                        return;
                    }
                }
            }
    
    

    利用多线程 异步上传:

    file

    这个如果设置了线程池的线程数量,并不能控制每次执行的线程数量限制在3个以内,因为异步的连接没有关掉导致了,暂时没有想到怎么关闭好,因为每次创建多个连接 会导致性能不佳(频繁多次连接消耗性能比较大),个人觉得如果是所有上传任务都结束了 再关掉开启的连接 这样是最后,如果你实现了这种方式,欢迎在下方留言。这边设置了ftp的最大连接数为3,如果线程池中线程超过3个,就会进入队列,等队列中有线程完成了,后面才会开启新的线程进入 这个队列(以此类推)。

    注意:request.KeepAlive为false, 最终所有连接都会自动关闭,但是频繁连接ftp性能会损耗,不适用太多的文件上传。

    下载功能:

    file

    当然如果是多线程也需要设置,因为这个下载是同步下载的。
    ThreadPool.SetMinThreads(1, 1); //设置最小线程数为1个
    ThreadPool.SetMaxThreads(5, 5); //设置最大线程数为5个,这两个方法要配合使用才能控制线程数量

    效果图:

    file

    源码地址:https://gitee.com/ten-ken/personal-manage.git

    功能示意

    file file

    至于断点续传,稍加改造就行了,等你发现哦!

    欢迎关注我的公众号:程序员ken,程序之路,让我们一起探索,共同进步。

    相关文章

      网友评论

          本文标题:ftp多线程上传、下载以及断点续传

          本文链接:https://www.haomeiwen.com/subject/fiozyltx.html