断点续传,下载。
private long tempStartPostion = 0; //开始位置 FileStream fs; if (File.Exists("D://test.zip")) { fs = System.IO.File.OpenWrite("D://test.zip"); tempStartPostion = fs.Length;// 文件流现在的大小 fs.Seek(tempStartPostion, System.IO.SeekOrigin.Current); //从开始位置移动到文件流中的当前位置 } else { fs = new System.IO.FileStream("D://test.zip", System.IO.FileMode.Create); tempStartPostion = 0; } Uri uri = new Uri("http://+路径");//http://www.cnblogs.com/wangliuzheng/ HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(uri); if (tempStartPostion > 0) { hwr.AddRange((int)tempStartPostion); } HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse(); long totalBytes = hwrs.ContentLength; Stream st = hwrs.GetResponseStream(); byte[] bt = new byte[1024]; int size = st.Read(bt, 0, (int)bt.Length); while (size > 0) { Application.DoEvents(); fs.Write(bt, 0, size); size = st.Read(bt, 0, bt.Length); } fs.Close(); st.Close(); }
wangliuzheng1990@126.com