使用C#通过FTP下载文件

3
作为一名初级开发人员,我需要找到一个使用ftp下载文件的解决方案。以下是我的代码:
虽然它可以工作,但有时候我无法打开下载的文件。
public static bool DownloadDocument(string ftpPath, string downloadPath) {
  bool retVal = false;
  try {
    Uri serverUri = new Uri(ftpPath);
    if (serverUri.Scheme != Uri.UriSchemeFtp) {
        return false;
    }
    FtpWebRequest reqFTP;
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
    reqFTP.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
    reqFTP.KeepAlive = false;
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    reqFTP.UseBinary = true;
    reqFTP.Proxy = null;
    reqFTP.UsePassive = false;

    using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) {
      using (Stream responseStream = response.GetResponseStream()) {
        using (FileStream writeStream = new FileStream(downloadPath, FileMode.Create)) {
          int Length = 1024 * 1024 * 30;
          Byte[] buffer = new Byte[Length];
          responseStream.Read(buffer, 0, Length);
        }
      }
    }
    retVal = true;
  }
  catch (Exception ex) {
    //Error logging to add
  }

  return retVal;
}

有什么想法,请提出!


你说的不太清楚。我已经发布了实际的代码。我的意思是有些文件无法打开。 - Tony_Clark
1
你的代码缺少对输出文件的实际写入。因此,说“有时它能工作”根本不正确。 - nvoigt
拥有空的 catch (Exception ex) 块是愚蠢的。即使你对其进行了处理,捕获一般的 Exception 已经够糟糕了。你应该尽量避免捕获这样的错误。 - Enigmativity
int Length = 1024 * 1024 * 30; 或许这就是问题所在。使用静态缓冲区似乎不太好。可能是缓冲区太小,因此文件没有完全写入。 - M Stoerzel
2个回答

6
为什么不使用Microsoft实现的WebClient下载FTP的功能呢?
using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("log", "pass");
    client.DownloadFile("ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

}

我该如何将文件保存到特定文件夹中? - Tony_Clark
@Tony_Clark,请检查第二个参数。它是本地文件名。 - Cetin Basoz

0

检查您无法打开的文件是否已损坏。 例如,FTP上的文件大小和本地PC上的文件大小是否相同?

您应该检查您的阅读器是否读取到了结尾!

public static bool DownloadDocument(string ftpPath, string downloadPath)
{
    bool retVal = false;
    try
    {
        Uri serverUri = new Uri(ftpPath);
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
        FtpWebRequest reqFTP;
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
        reqFTP.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.Proxy = null;
        reqFTP.UsePassive = false;

        using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (FileStream writeStream = new FileStream(downloadPath, FileMode.Create))
                {
                    int Length = 1024 * 1024 * 30;
                    Byte[] buffer = new Byte[Length];
                  int byteReads =  responseStream.Read(buffer, 0, Length);
                  while(byteReads > 0)
                  {
                      //Try like this
                      writeStream.Write(buffer, 0, byteReads);
                      bytesRead = responseStream.Read(buffer, 0, Length);
                  }

                }
            }
        }
        retVal = true;
    }
    catch (Exception ex)
    {
       //Error logging to add
    }

    return retVal;
}

我测试了一个有问题的文件,它可以工作!但是另一种解决方案更简单。谢谢。 - Tony_Clark
没问题,不用谢。 - Coskun Ozogul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接