FtpWebRequest下载文件

以下代码旨在通过FTP检索文件。 但是,我收到了一个错误。

serverPath = "ftp://xxxx/tmp/myfile.txt"; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath); request.KeepAlive = true; request.UsePassive = true; request.UseBinary = true; request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(username, password); // Read the file from the server & write to destination using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) using (StreamWriter destination = new StreamWriter(destinationFile)) { destination.Write(reader.ReadToEnd()); destination.Flush(); } 

错误是:

远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限)

该文件确实存在于远程计算机上,我可以手动执行此ftp(即我有权限)。 任何人都可以告诉我为什么我可能会收到此错误?

您可能会对FptWebRequest类引用中的这段内容感兴趣:

URI可以是相对的或绝对的。 如果URI的格式为“ ftp://contoso.com/%2fpath”(%2f是转义'/'),那么URI是绝对的,当前目录是/ path。 但是,如果URI的格式为“ ftp://contoso.com/path ”,则首先.NET Framework登录到FTP服务器(使用Credentials属性设置的用户名和密码),然后是当前目录设置为/ path。

我知道这是一个旧post,但我在这里添加以供将来参考。 这是我找到的解决方案:

  private void DownloadFileFTP() { string inputfilepath = @"C:\Temp\FileName.exe"; string ftphost = "xxx.xx.x.xxx"; string ftpfilepath = "/Updater/Dir1/FileName.exe"; string ftpfullpath = "ftp://" + ftphost + ftpfilepath; using (WebClient request = new WebClient()) { request.Credentials = new NetworkCredential("UserName", "P@55w0rd"); byte[] fileData = request.DownloadData(ftpfullpath); using (FileStream file = File.Create(inputfilepath)) { file.Write(fileData, 0, fileData.Length); file.Close(); } MessageBox.Show("Download Complete"); } } 

根据Ilya Kogan的出色建议更新

使用.NET框架从FTP服务器下载二进制文件的最简单方法是使用WebClient.DownloadFile

 WebClient client = new WebClient(); client.Credentials = new NetworkCredential("username", "password"); client.DownloadFile( "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip"); 

仅当您需要WebClient不提供的更大控件时才使用FtpWebRequest (如TLS / SSL加密,进度监控等)。 简单的方法是使用Stream.CopyTo将FTP响应流复制到FileStream

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.DownloadFile; using (Stream ftpStream = request.GetResponse().GetResponseStream()) using (Stream fileStream = File.Create(@"C:\local\path\file.zip")) { ftpStream.CopyTo(fileStream); } 

如果需要监视下载进度,则必须自己按块复制内容:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.DownloadFile; using (Stream ftpStream = request.GetResponse().GetResponseStream()) using (Stream fileStream = File.Create(@"C:\local\path\file.zip")) { byte[] buffer = new byte[10240]; int read; while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0) { fileStream.Write(buffer, 0, read); Console.WriteLine("Downloaded {0} bytes", fileStream.Position); } } 

对于GUI进度(WinForms ProgressBar ),请参阅:
使用ProgressBar进行FtpWebRequest FTP下载

如果要从远程文件夹下载所有文件,请参阅
C#通过FTP下载所有文件和子目录 。

我遇到过同样的问题!

我的解决方案是将public_html文件夹插入下载URL。

服务器上的真实文件位置:

myhost.com/public_html/myimages/image.png

url:

http://www.myhost.com/myimages/image.png

  private static DataTable ReadFTP_CSV() { String ftpserver = "ftp://servername/ImportData/xxxx.csv"; FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream responseStream = response.GetResponseStream(); // use the stream to read file from FTP StreamReader sr = new StreamReader(responseStream); DataTable dt_csvFile = new DataTable(); #region Code //Add Code Here To Loop txt or CSV file #endregion return dt_csvFile; } 

我希望它可以帮到你。

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath); 

在此之后,您可以使用以下行来避免错误..(访问被拒绝等)

 request.Proxy = null; 
  public void download(string remoteFile, string localFile) { private string host = "yourhost"; private string user = "username"; private string pass = "passwd"; private FtpWebRequest ftpRequest = null; private FtpWebResponse ftpResponse = null; private Stream ftpStream = null; private int bufferSize = 2048; try { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile); ftpRequest.Credentials = new NetworkCredential(user, pass); ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); ftpStream = ftpResponse.GetResponseStream(); FileStream localFileStream = new FileStream(localFile, FileMode.Create); byte[] byteBuffer = new byte[bufferSize]; int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); try { while (bytesRead > 0) { localFileStream.Write(byteBuffer, 0, bytesRead); bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); } } catch (Exception) { } localFileStream.Close(); ftpStream.Close(); ftpResponse.Close(); ftpRequest = null; } catch (Exception) { } return; }