读书人

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

发布时间: 2013-07-11 15:38:46 作者: rapoo

C#实现ftp下载支持断点续传
前几天做个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();
}

C# FTP 断点续传 winfrom 网络
[解决办法]
while的 iBytes?>=?0 应该只会执行一次吧?
你把调改改成 >= 要处理的文件的总大小呢?
[解决办法]
iBytes = soketData.Receive(buffer, buffer.Length, 0);发过来的数据都收完了,自然是等待接收新的数据了
续传你就把块号和偏移量记下
[解决办法]
同问顶起
!!!!!!!!!!!!!!!1

读书人网 >C#

热点推荐