我正在尝试从FTP下载文件。但是服务器返回错误550(找不到文件,没有访问权限) 在我的代码下面,
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream("d:/test/" +
"\\" +a , FileMode.Create,FileAccess.Write,FileShare.Read);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://example.com/" + " /" + a));
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
reqFTP.Credentials = new NetworkCredential("aaaa", "bbbb@1234");
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
// ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (WebException ex)
{
//MessageBox.Show(ex.Message);
String status = ((FtpWebResponse)ex.Response).StatusDescription;
MessageBox.Show(status);
}
文件可以在List中显示。虽然没有问题,但在下载时会抛出错误-550,在我的ftp中我有.txt,img,.pdf,.rar,.exe文件格式。
答案 0 :(得分:0)
看一下这篇文章:Using the C# WebClient class to upload and download FTP files
基本上,它依赖于执行所有网络工作的WebClient
对象。
该文章将其包装在自定义类中,该类与凭证一起使用。
本文的重要部分是:
public byte[] DownloadData(string path)
{
// Get the object used to communicate with the server.
WebClient request = new WebClient();
// Logon to the server using username + password
request.Credentials = new NetworkCredential(Username, Password);
return request.DownloadData(BuildServerUri(path));
}
确保凭据和路径正确无误。
答案 1 :(得分:0)
我只是在FTP地址中设置了确切的路径,并且设置缓冲区大小也很高,然后它的工作正常。
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://example.com/file path);
缓冲区大小,
byte[] buffer = new byte[32 * 1024];