【问题标题】:FTP Upload and Download - getting either 227 or 500 errorFTP 上传和下载 - 出现 227 或 500 错误
【发布时间】:2017-09-26 16:29:42
【问题描述】:

我想使用 FTP 上传和下载文件。我设法为我的上传和下载方法整理了以下代码。我被困在同一个地方。

如果我使用:

ftpRequest.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponseStream();

它给了我 500 错误。 但是,如果我只使用:

FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponseStream();

我得到:远程服务器返回错误:227 进入被动模式。

下载和上传方法的行为相同。我可以使用在线客户端上传文件,所以我知道服务器设置得很好。我按照某些线程的建议禁用了防病毒软件的防火墙,但这也不起作用。现在我不知道该怎么办。我的上传下载方式如下:

我的上传方式

private static void Upload ()
{
    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://ftp.myserver.com/");
    ftpRequest.Credentials = new NetworkCredential("username", "password");
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    ftpRequest.UsePassive = false;
    FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
    StreamReader streamReader = new StreamReader(response.GetResponseStream());

    string line = streamReader.ReadLine();
    while (!string.IsNullOrEmpty(line))
    {
        Console.WriteLine(line);
        line = streamReader.ReadLine();
    }

    streamReader.Close();
}

我的下载方法

FtpWebRequest reqFTP;
    try
        {
            FileStream outputStream = new FileStream(@"C:\download.csv", FileMode.Create);

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ftp.myserver.com/upload/myfile.csv"));
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = false;
            reqFTP.KeepAlive = true;
            reqFTP.Credentials = new NetworkCredential("username", "password");
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            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 (Exception ex)
        {

        }

【问题讨论】:

    标签: c# ftp-client ftpwebresponse


    【解决方案1】:

    这实际上是一个完美的解决方案。在我朋友的笔记本电脑上工作,但不是我的。似乎是一些防病毒设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 2011-10-08
      • 2013-09-25
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 2019-09-25
      相关资源
      最近更新 更多