从c#下载FTP,无法重试 [英] Downloading from FTP with c#, can't retry when fail
问题描述
很多时候我在文件下载时遇到连接问题,所以我必须重试,但我可以'不要这样做
当我尝试使用这一行再次下载de文件reader = response.GetResponseStream();我得到了跟随的消息进程无法访问文件xxx,因为它被另一个进程使用。我决定关闭作者流(memStream)在捕获,有时它的作品,但有时当我试图再次复制它不工作,给我一个超时的例外。
有没有一个明确的方法来重试从FTP没有错误下载文件?
编辑:我看到我可以再次下载第一个文件失败但是当另一个文件失败时,我无法从请求中获取响应并且gimme超时
这是代码:
FtpWebResponse response = null;
FtpWebRequest request = null;
Stream memStream = null;
Stream reader = null;
Directory.CreateDirectory(pathDestino);
try
{
request = FtpWebRequest.Create(urlArchivo)as FtpWebRequest;
request.Credentials = new NetworkCredential(ftp,ftp);
//现在获取实际数据
request = FtpWebRequest.Create(urlArchivo)为FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(ftp,ftp);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false; //完成时关闭连接
if(totalBytes!= 0)//检查文件是否失败
{
request.ContentOffset = totalBytes;
}
// Streams
response = request.GetResponse()as FtpWebResponse;
reader = response.GetResponseStream();
//下载到内存
//注意:在这里调整流直接下载到硬盘
// MemoryStream memStream = new MemoryStream();
string pathCompleto = pathDestino + @\+ horaMinuto +.zip;
memStream = File.Create(pathCompleto);
byte [] buffer = new byte [1024]; // chuncks下载
int bytesAcum = 0;
while(true)
{
int bytesRead = reader.Read(buffer,0,buffer.Length);
if(bytesRead == 0)
{
break;
}
else
{
//写下载数据
memStream.Write(buffer,0,bytesRead);
totalBytes = + bytesRead;
}
}
//将下载的流转换为字节数组
// downloadedData = memStream.ToArray();
//清理
reader.Close();
memStream.Close();
response.Close();
totalBytes = 0;
}
catch
{
// something
memStream.Close();
request.Abort();
}
通常FTP中的resume涉及从特定文件字节偏移量再次请求文件。
查看 FtpWebRequest
类,您需要的属性一旦连接失败,填充已经是您已经下载的缓冲区的长度,您应该设置属性 ContentOffset
。
有关此属性的更多信息,请参阅链接: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.contentoffset(v = vs.110).aspx
I have an application for download Zip files from an FTP.
Many times I got connection problems while a file is downloading so I have to retry but I can't do it.
When I try to download de file again with this line "reader = response.GetResponseStream();" I got the follow messagge "The process cannot access the file xxx because it is being used by another process". I decided to close the writer stream (memStream) in the catch and sometimes it works but sometimes when I try to get de respose again it doesn't work a give me a time out exception.
Is there an clear way to retry download a file from FTP without errors?
EDIT: I see that I can start download again the first file that fail but when another file fail I can't get the response from the request and gimme the time out
Here is the code:
FtpWebResponse response = null;
FtpWebRequest request = null;
Stream memStream = null;
Stream reader = null;
Directory.CreateDirectory(pathDestino);
try
{
request = FtpWebRequest.Create(urlArchivo) as FtpWebRequest;
request.Credentials = new NetworkCredential("ftp", "ftp");
//Now get the actual data
request = FtpWebRequest.Create(urlArchivo) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("ftp", "ftp");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false; //close the connection when done
if(totalBytes!=0) //check if a file failed
{
request.ContentOffset=totalBytes;
}
//Streams
response = request.GetResponse() as FtpWebResponse;
reader = response.GetResponseStream();
//Download to memory
//Note: adjust the streams here to download directly to the hard drive
//MemoryStream memStream = new MemoryStream();
string pathCompleto = pathDestino + @"\" + horaMinuto + ".zip";
memStream = File.Create(pathCompleto);
byte[] buffer = new byte[1024]; //downloads in chuncks
int bytesAcum = 0;
while (true)
{
int bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break;
}
else
{
//Write the downloaded data
memStream.Write(buffer, 0, bytesRead);
totalBytes=+bytesRead;
}
}
//Convert the downloaded stream to a byte array
//downloadedData = memStream.ToArray();
//Clean up
reader.Close();
memStream.Close();
response.Close();
totalBytes=0;
}
catch
{
//something
memStream.Close();
request.Abort();
}
Typically "resume" in FTP involves requesting the file again from a specific file byte offset.
Looking at FtpWebRequest
class, the property you need to populate once the connection fails will be the length of your already downloaded buffer and you should set the property ContentOffset
.
More information on this property at the link: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.contentoffset(v=vs.110).aspx
这篇关于从c#下载FTP,无法重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!