【问题标题】:Download file using ftp in c# [duplicate]在c#中使用ftp下载文件[重复]
【发布时间】:2019-01-02 05:33:22
【问题描述】:

作为一名初级开发人员,我应该找到使用 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;
}

请有任何想法!

【问题讨论】:

  • 不清楚你说什么。我发布了实际代码。我想说一些我无法打开的文件。
  • 您的代码缺少 any 实际 write 到您的输出文件。所以说“它有时有效”是不正确的。
  • 拥有空的catch (Exception ex) 块是愚蠢的。即使你用它做点什么,抓住一般的Exception 已经够糟糕的了。你真的应该避免捕获这样的错误。
  • int Length = 1024 * 1024 * 30; 也许这就是问题所在。拥有一个静态缓冲区似乎并不好。可能是缓冲区太小,因此文件没有完整写入。

标签: c# ftp


【解决方案1】:

你为什么不使用它? WebClient 由 Microsoft 实现,用于从 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,检查第二个参数。它是本地文件名。
【解决方案2】:

检查您无法打开的文件是否已损坏。 比如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;
}

【讨论】:

  • 我测试了一个有问题的文件,它可以工作!但另一种解决方案更容易。谢谢。
  • 没问题,欢迎您。
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2016-08-11
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多