C#实现ftp下载支持断点续传

.Net技术 码拜 8年前 (2015-11-24) 2312次浏览
前几天做个ftp下载小程序单位用,但是发现有时候文件下载不完整,本人查了老半天才发现时原因是网络延迟导致,解决方法是加个断线续传,爱本人去,一开始本人以为挺简单,做了两天参照网上的代码就是不行,才请各位老师们指点指点

if (a.ToString() != readCount.ToString())//断点续传
                {
                    Uri urii=new Uri("ftp://192.168.1.123:21");
                    FileInfo fileInfo = new FileInfo("E:\报价\巴比来定制产品.rar");
                    long locSize = fileInfo.Length;
                    RestartDownloadFromServer("巴比来定制产品.rar", urii, locSize);
                    CloseSocketConnect();
                    Socket soketData = CreateDataSocket();
                    Byte[] cmdType = GB2312.GetBytes(("TYPE I \r\n"));
                    socketControl.Send(cmdType, cmdType.Length, 0);
                    MessageBox.Show("开始断点续传");
                    FileStream output = new
                    FileStream("E:\报价\" + "巴比来定制产品.rar", FileMode.Append, FileAccess.Write);
                    Byte[] cmdPASV = GB2312.GetBytes(("PASV \r\n").ToCharArray());
                    socketControl.Send(cmdPASV, cmdPASV.Length, 0);
                    //获取本地文长度
                    FileInfo fileInfo = new FileInfo("E:\报价\巴比来定制产品.rar");
                    long locSize = fileInfo.Length;
                    MessageBox.Show(locSize.ToString());
                    //发送指令加文件长度
                    Byte[] cmdByte = GB2312.GetBytes(("REST " + locSize + " \r\n").ToCharArray());
                    socketControl.Send(cmdByte, cmdByte.Length, 0);
                    //发送指令加文件名和路径
                    Byte[] cmdRETR = GB2312.GetBytes(("RETR " + "E:\报价\巴比来定制产品.rar \r\n").ToCharArray());
                    socketControl.Send(cmdByte, cmdByte.Length, 0);
                    //下载.
                    int iBytes = 0;
                    int cnt = 0;
                    while (iBytes >= 0)
                    {
                        try
                        {
                            MessageBox.Show((cnt++).ToString());
                            iBytes = soketData.Receive(buffer, buffer.Length, 0);
                            output.Write(buffer, 0, iBytes);
                            //if (iBytes <= 0)
                            //{
                            //    break;
                            //}
                        }
                        catch
                        {
                        }
                    }
                    output.Close();
}
解决方案:30分
while的  iBytes >= 0 应该只会执行一次吧?
你把调改改成 >= 要处理的文件的总大小呢?
解决方案:40分
iBytes = soketData.Receive(buffer, buffer.Length, 0);发过来的数据都收完了,自然是等待接收新的数据了
续传你就把块号和偏移量记下
解决方案:30分
同问顶起
!1

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#实现ftp下载支持断点续传
喜欢 (0)
[1034331897@qq.com]
分享 (0)