从FTP C#下载大文件 [英] Download large file from FTP C#

查看:60
本文介绍了从FTP C#下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用示例代码通过FTP下载文件,因为我的旧代码不适用于较大的文件,因为它立即捕获整个文件,这会导致内存不足错误.

reader.read()要求提供(char [],int,int),就像我过去使用过的普通Read()方法一样,将其送入(byte [],int,int).

是否因为响应流而请求char []?以及如何创建与以前使用过的byte []类似的char []缓冲区?

我是否在正确的轨道上?


I am trying to download a file via FTP using sample code I found as my old code doesn''t work with larger files due to it grabbing the whole file at once which causes Out of Memory errors.

The reader.read() is asking for (char[],int,int) i''m feeding it (byte[],int,int) like normal Read() methods I have used in the past.

Is it asking for char[] because it''s a response stream? and How do I even create a char[] buffer that is simular to the byte[] I have used previously?

Am I even on the right track?


FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp01/temp.exe");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            request.Credentials = new NetworkCredential("admin", "1234");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            FileStream file = File.Create(@"c:\temp\temp.exe");
            byte[] buffer = new byte[32 * 1024];
            int read;
            reader.Read(
            while ((read = reader.Read(buffer,0,buffer.Length)) > 0)
            {
                file.Write(buffer, 0, read);
            }
            file.Close();
            reader.Close();
            response.Close();




在此先感谢您的帮助.




Thanks in advance for any help.

推荐答案

我认为您离我们太远了.除了不完整的行,我看不到有很多行不通的地方.您遇到错误了吗?

此处 [
I don''t think you''re too far off. Other than the incomplete line, I don''t see much that won''t work there. Are you getting an error?

Here''s[^] some code very close to what you''re doing (halfway down).


Try this instead...don''t go with a StreamReader, just use the Stream.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp01/temp.exe");
request.Method = WebRequestMethods.Ftp.DownloadFile;

request.Credentials = new NetworkCredential("admin", "1234");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
FileStream file = File.Create(@"c:\temp\temp.exe");
byte[] buffer = new byte[32 * 1024];
int read;
//reader.Read(

while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    file.Write(buffer, 0, read);
}

file.Close();
responseStream.Close();
response.Close();



干杯.



Cheers.


这篇关于从FTP C#下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆