2011-10-28 74 views
0

我想写一个程序,它将从FTP下载几个文件,将它们压缩后再上传到同一个FTP位置。FTP下载/上传错误504

我已经试图下载一个文件。如果失败,它会再试一次。

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

如果在下载时发生任何错误,它会在重新尝试时下载它们,但是无法上传。

我认为问题归咎于没有正确关闭连接,但我不能为我的生活弄清楚。

这是我的代码;我已经添加它失败:

上传:

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

} 

下载:

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

你得到了什么特定的例外? –

+0

System.Net.WebException:远程服务器返回错误:(503)错误的命令序列。 在System.Net.FtpWebRequest.SyncRequestCallback(对象OBJ) 在System.Net.FtpWebRequest.RequestCallback(对象OBJ) 在System.Net.CommandStream.InvokeRequestCallback(对象OBJ) 在System.Net.CommandStream.Abort(例外e) at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) at System.Net.FtpWebRequest.GetRequestStream() at RightMove.Form1.uploadFTP(String zip)in C:\ Users \ dir \ Form1.cs:line 407 – BenG

回答

0

对于初学者来说,你的包在流块using,使他们得到适当配置。

查看MSDN了解更多详情。

1

The error 504 – Command not implemented for that parameter.

暗示您在那里使用的某些选项并未由目标FTP服务器实现。我认为你的代码导致了一个奇怪的请求,建议看看你的进程在服务器端创建的FTP聊天记录。例如,服务器是否支持PASV模式? ACTV模式下的FTP协议(默认行为)总是很痛苦,因为它显式地使客户端在端口20上打开“文件接收端口”并进行监听。虽然大多数服务器都支持PASV模式传输,但如果不明确地将它们置于PASV模式,则会变得很痛苦。因此,看看聊天记录,看看服务器是否处于PASV模式,如果仍然有问题,请查看聊天记录,看看在FTP协商期间是否传递了“额外空间”。 FTP很笨,可能有几个缺陷。 :-)