C#实现ftp下载支持断点续传
前几天做个ftp下载小程序单位用,但是发现有时候文件下载不完整,我查了老半天才发现时因为网络延迟导致,解决方法是加个断线续传,爱我去,一开始我以为挺简单,做了两天参照网上的代码就是不行,才请各位老师们指点指点
if (a.ToString() != readCount.ToString())//断点续传C# FTP 断点续传 winfrom 网络
{
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();
}