FtpWebRequest下载文件不正确的大小

编程入门 行业动态 更新时间:2024-10-26 12:28:02
本文介绍了FtpWebRequest下载文件不正确的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用以下代码从远程ftp服务器下载文件:

FtpWebRequest请求=(FtpWebRequest) WebRequest.Create(SERVERPATH); request.KeepAlive = true; request.UsePassive = true; request.UseBinary = true; request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(userName,password);使用(StreamReader reader = new StreamReader(responseStream())返回 使用(FtpWebResponse响应=(FtpWebResponse)request.GetResponse()) using(Stream responseStream = response.GetResponseStream )) using(StreamWriter destination = new StreamWriter(destinationFile)) { destination.Write(reader.ReadToEnd()); destination.Flush(); }

我正在下载的文件是一个dll,我的问题是它正在被这个过程以某种方式改变。我知道这是因为文件的大小在增加。我怀疑这段代码有问题:

destination.Write(reader.ReadToEnd()); destination.Flush();

任何人都可以提供任何关于可能出错的想法吗? <解决方案 使用字符数据时,StreamReader 和 StreamWriter 所以你需要将字节流解码为字符,然后再将其编码回字节。一个dll文件包含二进制数据,所以这个往返转换会引入错误。您希望直接从 responseStream 对象中读取字节,并将其写入 FileStream 中,该文件不包含在的StreamWriter 。

如果您使用的是.NET 4.0,则可以使用 Stream.CopyTo ,否则您必须手动复制流。 这个StackOverflow问题有一个很好的方法来复制流: $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'新字节[32768]; while(true) { int read = input.Read(buffer,0,buffer.Length); if(读取< = 0) return; output.Write(buffer,0,read); $ b $ p $ b $ p所以,你的代码如下所示:使用(FtpWebResponse response =(FtpWebResponse)request.GetResponse()) using(Stream responseStream = response.GetResponseStream() ) using(FileStream destination = File.Create(destinationFile)) { CopyStream(responseStream,destination); }

I’m using the following code to download a file from a remote ftp server:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath); request.KeepAlive = true; request.UsePassive = true; request.UseBinary = true; request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(userName, password); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) using (StreamWriter destination = new StreamWriter(destinationFile)) { destination.Write(reader.ReadToEnd()); destination.Flush(); }

The file that I’m downloading is a dll and my problem is that it is being altered by this process in some way. I know this because the file size is increasing. I have a suspicion that this section of code is at fault:

destination.Write(reader.ReadToEnd()); destination.Flush();

Can anyone offer any ideas as to what may be wrong?

解决方案

StreamReader and StreamWriter work with character data, so you are decoding the stream from bytes to characters and then encoding it back to bytes again. A dll file contains binary data, so this round-trip conversion will introduce errors. You want to read bytes directly from the responseStream object and write to a FileStream that isn't wrapped in a StreamWriter.

If you are using .NET 4.0 you can use Stream.CopyTo, but otherwise you will have to copy the stream manually. This StackOverflow question has a good method for copying streams:

public static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[32768]; while (true) { int read = input.Read(buffer, 0, buffer.Length); if (read <= 0) return; output.Write(buffer, 0, read); } }

So, your code will look like this:

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (FileStream destination = File.Create(destinationFile)) { CopyStream(responseStream, destination); }

更多推荐

FtpWebRequest下载文件不正确的大小

本文发布于:2023-11-26 17:39:09,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   大小   文件   FtpWebRequest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!