Ric*_*yre 9 .net c# ftp ftpwebrequest c#-4.0
我正在尝试使用下载文件FtpWebRequest.
private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
int bytesRead = 0;
byte[] buffer = new byte[1024];
FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
request.Method = WebRequestMethods.Ftp.DownloadFile;
Stream reader = request.GetResponse().GetResponseStream();
BinaryWriter writer = new BinaryWriter(File.Open(localDestinationFilePath, FileMode.CreateNew));
while (true)
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
writer.Write(buffer, 0, bytesRead);
}
}
Run Code Online (Sandbox Code Playgroud)
它使用CreateFtpWebRequest我创建的这个方法:
private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive = false)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));
//Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
request.Proxy = null;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = keepAlive;
request.Credentials = new NetworkCredential(userName, password);
return request;
}
Run Code Online (Sandbox Code Playgroud)
它下载它.但信息总是被破坏.有谁知道发生了什么?
Ric*_*yre 25
刚想通了:
private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
int bytesRead = 0;
byte[] buffer = new byte[2048];
FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
request.Method = WebRequestMethods.Ftp.DownloadFile;
Stream reader = request.GetResponse().GetResponseStream();
FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);
while (true)
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
Run Code Online (Sandbox Code Playgroud)
不得不使用FileStream.
Mar*_*ryl 10
使用.NET框架从FTP服务器下载文件的最简单方法是使用WebClient.DownloadFile方法:
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
"ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");
Run Code Online (Sandbox Code Playgroud)
如果您只需要更大的控件,则使用FtpWebRequest类,WebClient该类不提供(如TLS/SSL加密,进度监视等).一种简单的方法是将FTP响应流复制到FileStream使用Stream.CopyTo方法:
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
ftpStream.CopyTo(fileStream);
}
Run Code Online (Sandbox Code Playgroud)
只有,如果您需要监控下载进度,您必须自己复制内容:
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
}
}
Run Code Online (Sandbox Code Playgroud)
有关GUI进度(WinForms ProgressBar),请参阅:
使用ProgressBar进行FtpWebRequest FTP下载
如果要从远程文件夹
下载所有文件,请参阅C#通过FTP下载所有文件和子目录.
| 归档时间: |
|
| 查看次数: |
48301 次 |
| 最近记录: |