0

为什么从 WebClient 流中读取时会丢失字节,如下所示?

const int chuckDim = 80;

System.Net.WebClient client = new System.Net.WebClient();
Stream stream = client.OpenRead("http://media-cdn.tripadvisor.com/media/photo-s/01/70/3e/a9/needed-backup-lol.jpg");
//Stream stream = client.OpenRead("file:///C:/Users/Tanganello/Downloads/needed-backup-lol.jpg");

//searching file length
WebHeaderCollection whc = client.ResponseHeaders;
int totalLength = (Int32.Parse(whc["Content-Length"]));
byte[] buffer = new byte[totalLength];

//reading and writing
FileStream filestream = new FileStream("C:\\Users\\Tanganello\\Downloads\\clone1.jpg", FileMode.Create, FileAccess.ReadWrite);
int accumulator = 0;
while (accumulator + chuckDim < totalLength) {
    stream.Read(buffer, accumulator, chuckDim);
    filestream.Write(buffer, accumulator, chuckDim);

    accumulator += chuckDim;
}
stream.Read(buffer, accumulator, totalLength - accumulator);
filestream.Write(buffer, accumulator, totalLength - accumulator);

stream.Close();
filestream.Flush();
filestream.Close();

这是我从第一个流中得到的:http: //img839.imageshack.us/img839/830/clone1h.jpg

4

1 回答 1

4

问题是您忽略了Stream.Read 方法的返回值:

数数

从当前流中读取的最大字节数。

返回值

读入缓冲区的总字节数。这可能小于请求的字节数


您可以通过简单地使用WebClient.DownloadFile 方法来避免读取和写入流的整个业务:

using (var client = new WebClient())
{
    client.DownloadFile(
        "http://media-cdn.tripadvisor.com/media/photo-s/01/70/3e/a9/needed-backup-lol.jpg",
        "C:\\Users\\Tanganello\\Downloads\\clone1.jpg");
}

或者,如果你真的想使用流,你可以简单地使用Stream.CopyTo 方法

using (var client = new WebClient())
using (var stream = client.OpenRead("http://..."))
using (var file = File.OpenWrite("C:\\..."))
{
    stream.CopyTo(file);
}

如果您坚持自己真正复制字节,那么正确的方法如下:

using (var client = new WebClient())
using (var stream = client.OpenRead("http://..."))
using (var file = File.OpenWrite("C:\\..."))
{
    var buffer = new byte[512];
    int bytesReceived;
    while ((bytesReceived = stream.Read(buffer, 0, buffer.Length)) != 0)
    {
        file.Write(buffer, 0, bytesReceived);
    }
}
于 2012-05-19T10:41:18.013 回答