我正在尝试使用示例代码通过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.
推荐答案我认为您离我们太远了.除了不完整的行,我看不到有很多行不通的地方.您遇到错误了吗? 此处 [ ^ ]一些与您正在做的事情非常接近的代码(降低一半). 改用此方法...不要与StreamReader一起使用,只需使用Stream. 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#下载大文件
发布评论