远程服务器返回错误(550)文件不可用(例如找不到文件无法访问)。
当我下载两个文件时,一个是21个字节的Date.txt文件,另一个是2 GB .zip文件。 Zip文件下载成功,但是在下载Date.txt时出现错误:
我正在从FTP文件下载中尝试此代码。:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string[] files = ReadFileList();
FTPSettings.IP = "xx.xx.xxx.xxx/TextureData";
FTPSettings.UserID = "xxxx";
FTPSettings.Password = "xxx";
//FtpWebRequest reqFTP = null;
//Stream ftpStream = null;
foreach (string file in files)
{
//string fileName = e.Argument.ToString();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPSettings.IP + "/" + file);
request.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Proxy = null;
long fileSize; // this is the key for ReportProgress
using (WebResponse resp = request.GetResponse())
fileSize = resp.ContentLength;
request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPSettings.IP + "/" + file);
request.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse responseFileDownload = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = responseFileDownload.GetResponseStream())
using (FileStream writeStream = new FileStream(@"C:\Program Files (x86)\xxxx\" + file, FileMode.Create))
{
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
int bytes = 0;
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
bytes += bytesRead;// don't forget to increment bytesRead !
int iProgress = 0;
int totalSize = (int)(fileSize) / 1000; // Kbytes
if (totalSize > 0)
{
iProgress = (bytes / 1000) * 100 / totalSize;
}
backgroundWorker1.ReportProgress(iProgress, totalSize);
}
}
string zipFileExtension = Path.GetExtension(file);
if (zipFileExtension == ".zip")
{
string zipToUnpack = @"C:\Program Files (x86)\xxxx\" + file;
string unpackDirectory = @"C:\Program Files (x86)\xxxx\";
using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
{
foreach (ZipEntry ze in zip1)
{
ze.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
}
}
}
}
}
public static class FTPSettings
{
public static string IP { get; set; }
public static string UserID { get; set; }
public static string Password { get; set; }
}
public string[] ReadFileList()
{
//Debugger.Break();
string[] mydownloadFiles;
StringBuilder myresult = new StringBuilder();
//WebResponse myresponse = null;
StreamReader myreader = null;
FtpWebRequest myreqFTP = null;
try
{
FTPSettings.IP = "xx.xx.xxx.xxx/TextureData";
FTPSettings.UserID = "xxxx";
FTPSettings.Password = "xxxx";
myreqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/"));
myreqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
myreqFTP.UseBinary = true;
myreqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
FtpWebResponse response = (FtpWebResponse)myreqFTP.GetResponse();
myreader = new StreamReader(response.GetResponseStream());
string myline = myreader.ReadLine();
while (myline != null)
{
myresult.Append(myline);
myresult.Append("\n");
myline = myreader.ReadLine();
}
// to remove the trailing '\n'
myresult.Remove(myresult.ToString().LastIndexOf('\n'), 1);
return myresult.ToString().Split('\n');
}
catch (Exception ex)
{
if (myreader != null)
{
myreader.Close();
}
mydownloadFiles = null;
return mydownloadFiles;
}
}