简体   繁体   中英

FTP download / upload error 504

I'm trying to write a program which will download a few files from and FTP, zip them up then upload them again to the same FTP location.

I have got it to attempt to download a file. If it fails, it will try again.

If NO errors occur, all files download and upload file.

If any errors occur when downloading, it will download them on the re-attempt, but fail then to upload.

I think the problems are down to not correctly closing a connection, but I can't for the life of me figure it out.

Here's my code; I've added where it fails:

Upload:

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)
{

}

Download:

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();
    }

The error 504 – Command not implemented for that parameter.

implies that some option you are using in there is not implemented by the target FTP server. I think that your code is resulting in a bizarre request, suggestion would be to look at the FTP chatter that your process creates on the server side. For exmaple, does the server support PASV mode? FTP protocol in ACTV mode (the default behavior) is always a pain because it explicitly causes the client to open a "file receive port" on port 20 and listen. While most servers support PASV mode transfers, it can become a pain if you don't put them in the PASV mode explicitly. So look at the chatter, see if the server is in PASV mode, and if you still have trouble, look at the chatter to see if there are "Extra Spaces" passed on during FTP negotiation. FTP is quite dinky and there can be several pitfalls. :-)

For starters, wrap your streams in using blocks so that they are disposed appropriately.

See MSDN for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM