【发布时间】:2017-06-01 19:05:21
【问题描述】:
我可以创建一个文件夹,我可以重命名文件,但我不能上传和下载文件到 ftp。显示的异常是 System.Net.WebException:远程服务器返回错误:(500) 语法错误,命令无法识别。
有人知道为什么吗?
日志文件:
System.Net Information: 0 : [6112] FtpControlStream#7746814 - Resposta recebida [500 PORT/EPRT (Active Mode/Extended Active Mode) is not supported. Use PASV/EPSV instead of this]
System.Net Information: 0 : [6112] FtpWebRequest#30923613::(Liberando a conexão de FTP#7746814.)
System.Net Error: 0 : [6112] Exceção em FtpWebRequest#30923613::GetRequestStream - O servidor remoto retornou um erro: (500) Erro de sintaxe, comando não reconhecido.
代码:
/* Upload File */
public void upload(string remoteFile, string localFile)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + remoteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = false;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
/* Establish Return Communication with the FTP Server */
Stream ftpStream = ftpRequest.GetRequestStream();
/* Open a File Stream to Read the File for Upload */
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
/* Buffer for the Downloaded Data */
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
/* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent);
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
【问题讨论】:
-
即使用
ftpRequest.UsePassive = true;,除非您有非常好的理由不这样做。 -
如果我使用 ftpRequest.UsePassive = true;显示的异常是:远程服务器返回错误:227 Entering Passive Mode (201,23,75,26,225,86)
-
您是否尝试用谷歌搜索该错误消息?如果您这样做了并且建议的解决方案都没有帮助,请针对新代码和错误编辑您的问题。并确保包含一个日志文件,至少:stackoverflow.com/q/9664650/850848
-
命令出现错误:Stream ftpStream = ftpRequest.GetRequestStream();
-
对不起,这是我第一次在这里提出问题。我已经阅读了几篇文章,但没有找到解决此问题的方法。每次在流中传入逗号 ftpStream = ftpRequest.GetRequestStream();引发异常。我已经在另一个 ftp 上对其进行了测试,并且发生了同样的问题。我不知道还能做什么。
标签: c# download ftp filestream ftpwebrequest