FtpWebRequest Download File Incorrect Size(FtpWebRequest 下载文件大小不正确)
问题描述
我正在使用以下代码从远程 ftp 服务器下载文件:
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();
}
我正在下载的文件是一个 dll,我的问题是它被这个过程以某种方式改变了.我知道这是因为文件大小在增加.我怀疑这部分代码有问题:
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
和 StreamWriter
处理字符数据,因此您正在将流从字节解码为字符并然后再次将其编码回字节.dll 文件包含二进制数据,因此这种往返转换会引入错误.您想直接从 responseStream
对象读取字节并写入未包装在 StreamWriter
中的 FileStream
.
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
.
如果您使用的是 .NET 4.0,则可以使用 Stream.CopyTo
,否则您将不得不手动复制流.这个StackOverflow问题有一个很好的复制流的方法:
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 下载文件大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FtpWebRequest 下载文件大小不正确


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01