上传性能
首先退出默认的配置项(这部分有对应的办法进行设置):
异步上传的局部代码
/// <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; } } }
利用多线程 异步上传:
这个如果设置了线程池的线程数量,并不能管制每次执行的线程数量限度在3个以内,因为异步的连贯没有关掉导致了,临时没有想到怎么敞开好,因为每次创立多个连贯 会导致性能不佳(频繁屡次连贯耗费性能比拟大),集体感觉如果是所有上传工作都完结了 再关掉开启的连贯 这样是最初,如果你实现了这种形式,欢送在下方留言。这边设置了ftp的最大连接数为3,如果线程池中线程超过3个,就会进入队列,等队列中有线程实现了,前面才会开启新的线程进入 这个队列(以此类推)。
留神:request.KeepAlive为false, 最终所有连贯都会主动敞开,然而频繁连贯ftp性能会损耗,不实用太多的文件上传。
下载性能:
当然如果是多线程也须要设置,因为这个下载是同步下载的。
ThreadPool.SetMinThreads(1, 1); //设置最小线程数为1个 ThreadPool.SetMaxThreads(5, 5); //设置最大线程数为5个,这两个办法要配合应用能力控制线程数量
效果图:
源码地址:https://gitee.com/ten-ken/personal-manage.git
性能示意
至于断点续传,稍加革新就行了,等你发现哦!
欢送关注我的公众号:程序员ken,程序之路,让咱们一起摸索,共同进步。