首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FTP下载/上传错误504

问FTP下载/上传错误504
EN

Stack Overflow用户
提问于 2011-10-29 03:49:42
回答 2查看 3K关注 0票数 0

我正在试着写一个程序,将下载几个文件从和FTP,压缩他们,然后再次上传到相同的FTP位置。

我让它尝试下载一个文件。如果失败,它将重试。

如果没有错误发生,所有文件都会下载并上传文件。

如果在下载时出现任何错误,它将在重新尝试时下载它们,但随后无法上载。

我认为问题在于没有正确地关闭连接,但我无论如何也找不到答案。

以下是我的代码;我添加了它失败的地方:

上传:

代码语言:javascript
复制
FileInfo fileInf = new FileInfo("directory" + zip + ".zip");
string uri = "ftp://address" + fileInf.Name;
FtpWebRequest reqFTP2;
reqFTP2 = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://address" + fileInf.Name));
reqFTP2.Credentials = new NetworkCredential("username", "password");
reqFTP2.KeepAlive = true;
reqFTP2.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP2.UseBinary = true;
reqFTP2.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
    Stream strm = reqFTP2.GetRequestStream(); //FAILS HERE
    contentLen = fs.Read(buff, 0, buffLength);
    while (contentLen != 0)
    {
        strm.Write(buff, 0, contentLen);
        contentLen = fs.Read(buff, 0, buffLength);
    }
    strm.Close();
    fs.Close();

}
catch (Exception ex)
{

}

下载:

代码语言:javascript
复制
int errorOccured = 0;
while (errorOccured < 1)
{
    FileStream outputStream = new FileStream("directory\\" + file, FileMode.Create);
    FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://address/" + file));
    reqFTP.Credentials = new NetworkCredential("username", "password");
    try
    {
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        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();
        errorOccured++;
    }
    catch (Exception er)
    {
        outputStream.Close();
    }
EN

回答 2

Stack Overflow用户

发布于 2011-10-29 04:01:40

未为该参数实施错误504 - Command

。

表示目标FTP服务器未实现您在其中使用的某些选项。我认为你的代码导致了一个奇怪的请求,建议你看看你的进程在服务器端创建的FTP聊天。例如,服务器是否支持PASV模式?ACTV模式下的FTP协议(默认行为)总是令人头疼,因为它会显式地导致客户端在端口20上打开“文件接收端口”并进行侦听。虽然大多数服务器都支持PASV模式传输,但如果不显式地将它们设置为PASV模式,这可能会成为一种痛苦。因此,请查看聊天记录,查看服务器是否处于PASV模式,如果仍有问题,请查看聊天记录,以查看在FTP协商期间是否传递了“额外的空格”。FTP相当微不足道,可能有几个陷阱。:-)

票数 1
EN

Stack Overflow用户

发布于 2011-10-29 04:01:30

对于初学者,将流包装在using块中,以便它们得到适当的处理。

有关更多详细信息,请参阅MSDN。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7934117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档