C#限速下载网络文件的方法实例

C#限速下载网络文件的方法,具体如下:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common.Utils;
using Utils;

namespace 爬虫
{
  public partial class Form1 : Form
  {
    #region 变量
    /// <summary>
    /// 已完成字节数
    /// </summary>
    private long completedCount = 0;
    /// <summary>
    /// 是否完成
    /// </summary>
    private bool isCompleted = true;
    /// <summary>
    /// 数据块队列
    /// </summary>
    private ConcurrentQueue<MemoryStream> msQueue = new ConcurrentQueue<MemoryStream>();
    /// <summary>
    /// 下载开始位置
    /// </summary>
    private long range = 0;
    /// <summary>
    /// 文件大小
    /// </summary>
    private long total = 0;
    /// <summary>
    /// 一段时间内的完成节点数,计算网速用
    /// </summary>
    private long unitCount = 0;
    /// <summary>
    /// 上次计时时间,计算网速用
    /// </summary>
    private DateTime lastTime = DateTime.MinValue;
    /// <summary>
    /// 一段时间内的完成字节数,控制网速用
    /// </summary>
    private long unitCountForLimit = 0;
    /// <summary>
    /// 上次计时时间,控制网速用
    /// </summary>
    private DateTime lastTimeForLimit = DateTime.MinValue;
    /// <summary>
    /// 下载文件sleep时间,控制速度用
    /// </summary>
    private int sleepTime = 1;
    #endregion

    #region Form1
    public Form1()
    {
      InitializeComponent();
    }
    #endregion

    #region Form1_Load
    private void Form1_Load(object sender, EventArgs e)
    {
      lblMsg.Text = string.Empty;
      lblByteMsg.Text = string.Empty;
      lblSpeed.Text = string.Empty;
    }
    #endregion

    #region Form1_FormClosing
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

    }
    #endregion

    #region btnDownload_Click 下载
    private void btnDownload_Click(object sender, EventArgs e)
    {
      isCompleted = false;
      btnDownload.Enabled = false;
      string url = txtUrl.Text.Trim();
      string filePath = CreateFilePath(url);

      #region 下载线程
      Thread thread = new Thread(new ThreadStart(() =>
      {
        int tryTimes = 0;
        while (!HttpDownloadFile(url, filePath))
        {
          Thread.Sleep(10000);

          tryTimes++;
          LogUtil.Log("请求服务器失败,重新请求" + tryTimes.ToString() + "次");
          this.Invoke(new InvokeDelegate(() =>
          {
            lblMsg.Text = "请求服务器失败,重新请求" + tryTimes.ToString() + "次";
          }));
          HttpDownloadFile(url, filePath);
        }
      }));
      thread.IsBackground = true;
      thread.Start();
      #endregion

      #region 保存文件线程
      thread = new Thread(new ThreadStart(() =>
      {
        while (!isCompleted)
        {
          MemoryStream ms;
          if (msQueue.TryDequeue(out ms))
          {
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write))
            {
              fs.Seek(completedCount, SeekOrigin.Begin);
              fs.Write(ms.ToArray(), 0, (int)ms.Length);
              fs.Close();
            }
            completedCount += ms.Length;
          }

          if (total != 0 && total == completedCount)
          {
            Thread.Sleep(100);
            isCompleted = true;
          }

          Thread.Sleep(1);
        }
      }));
      thread.IsBackground = true;
      thread.Start();
      #endregion

      #region 计算网速/进度线程
      thread = new Thread(new ThreadStart(() =>
      {
        while (!isCompleted)
        {
          Thread.Sleep(1000);

          if (lastTime != DateTime.MinValue)
          {
            double sec = DateTime.Now.Subtract(lastTime).TotalSeconds;
            double speed = unitCount / sec / 1024;

            try
            {
              #region 显示速度
              if (speed < 1024)
              {
                this.Invoke(new InvokeDelegate(() =>
                {
                  lblSpeed.Text = string.Format("{0}KB/S", speed.ToString("0.00"));
                }));
              }
              else
              {
                this.Invoke(new InvokeDelegate(() =>
                {
                  lblSpeed.Text = string.Format("{0}MB/S", (speed / 1024).ToString("0.00"));
                }));
              }
              #endregion

              #region 显示进度
              this.Invoke(new InvokeDelegate(() =>
              {
                string strTotal = (total / 1024 / 1024).ToString("0.00") + "MB";
                if (total < 1024 * 1024)
                {
                  strTotal = (total / 1024).ToString("0.00") + "KB";
                }
                string completed = (completedCount / 1024 / 1024).ToString("0.00") + "MB";
                if (completedCount < 1024 * 1024)
                {
                  completed = (completedCount / 1024).ToString("0.00") + "KB";
                }
                lblMsg.Text = string.Format("进度:{0}/{1}", completed, strTotal);
                lblByteMsg.Text = string.Format("已下载:{0}\r\n总大小:{1}", completedCount, total);

                if (completedCount == total)
                {
                  MessageBox.Show("完成");
                }
              }));
              #endregion
            }
            catch { }

            lastTime = DateTime.Now;
            unitCount = 0;
          }
        }
      }));
      thread.IsBackground = true;
      thread.Start();
      #endregion

      #region 限制网速线程
      thread = new Thread(new ThreadStart(() =>
      {
        while (!isCompleted)
        {
          Thread.Sleep(100);

          if (lastTimeForLimit != DateTime.MinValue)
          {
            double sec = DateTime.Now.Subtract(lastTimeForLimit).TotalSeconds;
            double speed = unitCountForLimit / sec / 1024;

            try
            {
              #region 限速/解除限速
              double limitSpeed = 0;
              if (double.TryParse(txtSpeed.Text.Trim(), out limitSpeed))
              {
                if (speed > limitSpeed && sleepTime < 1000)
                {
                  sleepTime += 1;
                }
                if (speed < limitSpeed - 10 && sleepTime >= 2)
                {
                  sleepTime -= 1;
                }
              }
              else
              {
                this.Invoke(new InvokeDelegate(() =>
                {
                  txtSpeed.Text = "100";
                }));
              }
              #endregion
            }
            catch { }

            lastTimeForLimit = DateTime.Now;
            unitCountForLimit = 0;
          }
        }
      }));
      thread.IsBackground = true;
      thread.Start();
      #endregion

    }
    #endregion

    #region HttpDownloadFile 下载文件
    /// <summary>
    /// Http下载文件
    /// </summary>
    public bool HttpDownloadFile(string url, string filePath)
    {
      try
      {
        if (!File.Exists(filePath))
        {
          using (FileStream fs = new FileStream(filePath, FileMode.Create))
          {
            fs.Close();
          }
        }
        else
        {
          FileInfo fileInfo = new FileInfo(filePath);
          range = fileInfo.Length;
        }

        // 设置参数
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)";
        request.Proxy = null;
        //发送请求并获取相应回应数据
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        if (response.ContentLength == range)
        {
          this.Invoke(new InvokeDelegate(() =>
          {
            lblMsg.Text = "文件已下载";
          }));
          return true;
        }

        // 设置参数
        request = WebRequest.Create(url) as HttpWebRequest;
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)";
        request.Proxy = null;
        request.AddRange(range);
        //发送请求并获取相应回应数据
        response = request.GetResponse() as HttpWebResponse;
        //直到request.GetResponse()程序才开始向目标网页发送Post请求
        Stream responseStream = response.GetResponseStream();

        total = range + response.ContentLength;
        completedCount = range;

        MemoryStream ms = new MemoryStream();
        byte[] bArr = new byte[1024];
        lastTime = DateTime.Now;
        lastTimeForLimit = DateTime.Now;
        int size = responseStream.Read(bArr, 0, (int)bArr.Length);
        unitCount += size;
        unitCountForLimit += size;
        ms.Write(bArr, 0, size);
        while (!isCompleted)
        {
          size = responseStream.Read(bArr, 0, (int)bArr.Length);
          unitCount += size;
          unitCountForLimit += size;
          ms.Write(bArr, 0, size);
          if (ms.Length > 102400)
          {
            msQueue.Enqueue(ms);
            ms = new MemoryStream();
          }
          if (completedCount + ms.Length == total)
          {
            msQueue.Enqueue(ms);
            ms = new MemoryStream();
          }

          Thread.Sleep(sleepTime);
        }
        responseStream.Close();
        return true;
      }
      catch (Exception ex)
      {
        LogUtil.LogError(ex.Message + "\r\n" + ex.StackTrace);
        return false;
      }
    }
    #endregion

    #region 根据URL生成文件保存路径
    private string CreateFilePath(string url)
    {
      string path = Application.StartupPath + "\\download";
      if (!Directory.Exists(path))
      {
        Directory.CreateDirectory(path);
      }

      string fileName = Path.GetFileName(url);
      if (fileName.IndexOf("?") > 0)
      {
        return path + "\\" + fileName.Substring(0, fileName.IndexOf("?"));
      }
      else
      {
        return path + "\\" + fileName;
      }
    }
    #endregion

  } //end Form1类
}

测试截图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#实现附件上传和下载功能

    通常情况下,我们会遇到各种上传附件的情况,以及上传后需要下载,文档格式各种各样,当然这个过程中也是报不同错误,还是一个原则,具体问题,具体分析:需求图: 上传代码实现:  aspx代码: <asp:Panel ID="Panel5" runat="server"> <fieldset> <legend>整体活动效果:</legend> <nav class="navbar navbar-default&

  • C#实现文件上传与下载功能实例

    最近学习了 C#实现文件上传与下载,现在分享给大家. 1.C#文件上传 创建MyUpload.htm页面,用于测试 <form name="form1" method="post" action="UploadFile.aspx" id="form1" enctype="multipart/form-data"> <input type="file" id="

  • C#实现文件断点续传下载的方法

    本文实例讲述了C#实现文件断点续传下载的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.IO; using System.Text; using System.Net; namespace simpleDemo { class Program { /// <su

  • C#实现多线程下载文件的方法

    本文实例讲述了C#实现多线程下载文件的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; using System.Net; namespace WfpApp { public class MultiDownload { #region 变量 pri

  • C#实现把图片下载到服务器代码

    C#实现把图片下载到服务器代码 ASPX页面代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetPictureByUrl.aspx.cs" Inherits="HoverTreeMobile.GetPictureByUrl" %> <!DOCTYPE html> <html xmlns="http://www.

  • C#限速下载网络文件的方法实例

    C#限速下载网络文件的方法,具体如下: using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; us

  • Java读取并下载网络文件的方法

    本文实例为大家分享了Java读取并下载网络文件的具体代码,供大家参考,具体内容如下 import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /**

  • 使用urllib库的urlretrieve()方法下载网络文件到本地的方法

    概述 见源码 源码 # !/usr/bin/env python # -*- coding:utf-8 -*- """ 图片(文件)下载,核心方法是 urllib.urlrequest 模块的 urlretrieve()方法 urlretrieve(url, filename=None, reporthook=None, data=None) url: 文件url filename: 保存到本地时,使用的文件(路径)名称 reporthook: 文件传输时的回调函数 data

  • JSP下载服务器文件的方法

    本文实例讲述了JSP下载服务器文件的方法.分享给大家供大家参考,具体如下: <%@page import="java.io.FileInputStream"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@page

  • Python使用wget实现下载网络文件功能示例

    本文实例讲述了Python使用wget实现下载网络文件功能.分享给大家供大家参考,具体如下: wget是一个从网络上自动下载文件的自由工具.它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理. ubuntu 安装wget pip install wget 从网络或本地硬盘下载文件(并解压) # -*- coding: utf-8 -*- import wget, tarfile import os # 网络地址 DATA_URL = 'http://www.robots.ox.ac.u

  • Java用文件流下载网络文件示例代码

    复制代码 代码如下: public HttpServletResponse download(String path, HttpServletResponse response) {        try {            // path是指欲下载的文件的路径.            File file = new File(path);            // 取得文件名.            String filename = file.getName();          

  • Android 使用URLConnection下载音频文件的方法

    使用MediaPlayer播放在线音频,请参考Android MediaPlayer 播放音频 有时候我们会需要下载音频文件.这里提供一种思路,将在线音频文件通过流写到本地文件中. 使用URLConnection来建立连接,获取到的数据写到文件中. URLConnection建立连接后,可以获取到数据长度.由此我们可以计算出下载进度. public class DownloadStreamThread extends Thread { String urlStr; final String ta

  • java读取properties文件的方法实例分析

    本文实例讲述了java读取properties文件的方法.分享给大家供大家参考.具体分析如下: 1.不在项目中读取: Properties properties = new Properties(); BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream("文件的路径"),"utf-8")); properties.load(read); properti

  • python复制文件的方法实例详解

    本文实例讲述了python复制文件的方法.分享给大家供大家参考.具体分析如下: 这里涉及Python复制文件在实际操作方案中的实际应用以及Python复制文件 的相关代码说明,希望你会有所收获. Python复制文件: import shutil import os import os.path src = " d:\\download\\test\\myfile1.txt " dst = " d:\\download\\test\\myfile2.txt " ds

  • jQuery加载及解析XML文件的方法实例分析

    本文实例讲述了jQuery加载及解析XML文件的方法.分享给大家供大家参考,具体如下: 1.简述 XML(eXtensible Markup Language)即可扩展标记语言,与HTML一样,都是属于SGML标准通用语言. 2. Content-Type 很多情况下XML文件不能正常解析都是由于Content-Type没有设置好.如果Content-Type本身就是一个XML文件则不需要设置:如果是由后台程序动态生成的,那么就需要设置Content-Type为"text/xml",否

随机推荐