• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

.net wpf

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

断点续传,下载。

            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

posted on 2013-04-08 15:09  王留争  阅读(722)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3