0
我想使用FTP上傳和下載文件。我設法將我的上傳和下載方法的代碼放在一起。我被困在同一個地方。FTP上傳和下載 - 獲取227或500錯誤
如果我使用:
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)
{
}