本文介绍了无法从FTP下载.zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi,
private void Download()
{
Uri url = new Uri("ftp://"+FtpAddress+"/filename");
if (url.Scheme == Uri.UriSchemeFtp)
{
FtpWebRequest objRequest = (FtpWebRequest)FtpWebRequest.Create(url);
//Set credentials if required else comment this Credential code
NetworkCredential objCredential = new NetworkCredential(sFtpUsername,sFtpPassword);
objRequest.Credentials = objCredential;
objRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse objResponse = (FtpWebResponse)objRequest.GetResponse();
StreamReader objReader = new StreamReader(objResponse.GetResponseStream());
byte[] buffer = new byte[16 * 1024];
int len = 0;
FileStream objFS = new FileStream("Test.jpg", FileMode.Create, FileAccess.Write, FileShare.Read);
while ((len = objReader.BaseStream.Read(buffer, 0, buffer.Length)) != 0)
{
objFS.Write(buffer, 0, len);
}
objFS.Close();
objResponse.Close();
}
}
我使用上面的代码从FTP服务器下载文件。我能够下载文件而不是.zip,.rar文件。如何从FTP下载.zip,.rar文件请帮我解决这个问题。当我尝试下载.zip文件时,我收到以下错误远程服务器返回错误:(502)Bad Gateway。
I use the above code to download files from FTP server. I am able to download the files not .zip,.rar files. How to download .zip,.rar files from FTP please help me to solve this issue. When I try to download .zip file, I get the following error The remote server returned an error: (502) Bad Gateway.
推荐答案
public static List<periodicalfile> GetFTPFilesPath(string ftpAddress, string UserName, string Password)
{
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpAddress);
try
{
reqFTP.UsePassive = true;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(UserName, Password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
List<string> files = new List<string>();
StreamReader reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
files.Add(reader.ReadLine());
reader.Close();
responseStream.Dispose();
//Loop through the resulting file names.
string ftpPath = string.Empty;
foreach (var fileName in files)
{
var parentDirectory = "";
PeriodicalFile lstFile = new PeriodicalFile();
//If the filename has an extension, then it actually is
//a file
if (fileName.Contains(".zip"))
{
ftpPath = ftpAddress + fileName;
lstFile.FTPFileName = ftpPath;
listAllFTPFolder.Add(lstFile);
}
else
{
//If the filename has no extension, then it is just a folder.
//Run this method again as a recursion of the original:
parentDirectory += fileName + "/";
try
{
GetFTPFilesPath(ftpAddress + "/" + parentDirectory, UserName, Password);
}
catch (Exception ex)
{
ErrorLog.WriteLog("ftpFileProcessing", "GetFTPFilesPath(Else)", ex.Message, ex.StackTrace, "");
}
}
}
}
catch (Exception excpt)
{
reqFTP.Abort();
ErrorLog.WriteLog("ftpFileProcessing", "GetFTPFilesPath", excpt.Message, excpt.StackTrace, "");
}
return listAllFTPFolder;
Uri url = new Uri("ftp://ftp.demo.com/file1.txt");
if (url.Scheme == Uri.UriSchemeFtp)
{
FtpWebRequest objRequest = (FtpWebRequest)FtpWebRequest.Create(url);
//Set credentials if required else comment this Credential code
NetworkCredential objCredential = new NetworkCredential("FTPUserName", "FTPPassword");
objRequest.Credentials = objCredential;
objRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse objResponse = (FtpWebResponse)objRequest.GetResponse();
StreamReader objReader = new StreamReader(objResponse.GetResponseStream());
byte[] buffer = new byte[16 * 1024];
int len = 0;
FileStream objFS = new FileStream(Server.MapPath("file1.txt"), FileMode.Create, FileAccess.Write, FileShare.Read);
while ((len = objReader.BaseStream.Read(buffer, 0, buffer.Length)) != 0)
{
objFS.Write(buffer, 0, len);
}
objFS.Close();
objResponse.Close();
}
[]
public static void DownloadFTPFiles(string ftpAddress, string UserName, string Password)
{
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpAddress);
try
{
reqFTP.UsePassive = true;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(UserName, Password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
List<string> files = new List<string>();
StreamReader reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
files.Add(reader.ReadLine());
reader.Close();
responseStream.Dispose();
List<periodicalfile> lstFiles = new List<periodicalfile>();
//Loop through the resulting file names.
string ftpPath = string.Empty;
foreach (var fileName in files)
{
var parentDirectory = "";
if (fileName.Contains(".zip"))
{
ftpPath = ftpAddress + fileName;
wc.Credentials = new NetworkCredential(UserName, Password);
wc.DownloadFile(file.FTPFileName, DestinationFolder + fileName);//DOWLOAD FROM FTP
}
else
{
//If the filename has no extension, then it is just a folder.
//Run this method again as a recursion of the original:
parentDirectory += fileName + "/";
try
{
DownloadFTPFiles(ftpAddress + "/" + parentDirectory, UserName, Password);
}
catch (Exception ex)
{
ErrorLog.WriteLog("ftpFileProcessing", "DownloadFTPFiles(Else)", ex.Message, ex.StackTrace, "");
}
}
}
}
catch (Exception excpt)
{
reqFTP.Abort();
ErrorLog.WriteLog("ftpFileProcessing", "DownloadFTPFiles", excpt.Message, excpt.StackTrace, "");
}
}
这篇关于无法从FTP下载.zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!