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 = ;
/// <summary>
/// 是否完成
/// </summary>
private bool isCompleted = true;
/// <summary>
/// 数据块队列
/// </summary>
private ConcurrentQueue<MemoryStream> msQueue = new ConcurrentQueue<MemoryStream>();
/// <summary>
/// 下载开始位置
/// </summary>
private long range = ;
/// <summary>
/// 文件大小
/// </summary>
private long total = ;
/// <summary>
/// 一段时间内的完成节点数,计算网速用
/// </summary>
private long unitCount = ;
/// <summary>
/// 上次计时时间,计算网速用
/// </summary>
private DateTime lastTime = DateTime.MinValue;
/// <summary>
/// 一段时间内的完成字节数,控制网速用
/// </summary>
private long unitCountForLimit = ;
/// <summary>
/// 上次计时时间,控制网速用
/// </summary>
private DateTime lastTimeForLimit = DateTime.MinValue;
/// <summary>
/// 下载文件sleep时间,控制速度用
/// </summary>
private int sleepTime = ;
#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 = ;
while (!HttpDownloadFile(url, filePath))
{
Thread.Sleep(); 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(), , (int)ms.Length);
fs.Close();
}
completedCount += ms.Length;
} if (total != && total == completedCount)
{
Thread.Sleep();
isCompleted = true;
} Thread.Sleep();
}
}));
thread.IsBackground = true;
thread.Start();
#endregion #region 计算网速/进度线程
thread = new Thread(new ThreadStart(() =>
{
while (!isCompleted)
{
Thread.Sleep(); if (lastTime != DateTime.MinValue)
{
double sec = DateTime.Now.Subtract(lastTime).TotalSeconds;
double speed = unitCount / sec / ; try
{
#region 显示速度
if (speed < )
{
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 / ).ToString("0.00"));
}));
}
#endregion #region 显示进度
this.Invoke(new InvokeDelegate(() =>
{
string strTotal = (total / / ).ToString("0.00") + "MB";
if (total < * )
{
strTotal = (total / ).ToString("0.00") + "KB";
}
string completed = (completedCount / / ).ToString("0.00") + "MB";
if (completedCount < * )
{
completed = (completedCount / ).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 = ;
}
}
}));
thread.IsBackground = true;
thread.Start();
#endregion #region 限制网速线程
thread = new Thread(new ThreadStart(() =>
{
while (!isCompleted)
{
Thread.Sleep(); if (lastTimeForLimit != DateTime.MinValue)
{
double sec = DateTime.Now.Subtract(lastTimeForLimit).TotalSeconds;
double speed = unitCountForLimit / sec / ; try
{
#region 限速/解除限速
double limitSpeed = ;
if (double.TryParse(txtSpeed.Text.Trim(), out limitSpeed))
{
if (speed > limitSpeed && sleepTime < )
{
sleepTime += ;
}
if (speed < limitSpeed - && sleepTime >= )
{
sleepTime -= ;
}
}
else
{
this.Invoke(new InvokeDelegate(() =>
{
txtSpeed.Text = "";
}));
}
#endregion
}
catch { } lastTimeForLimit = DateTime.Now;
unitCountForLimit = ;
}
}
}));
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[];
lastTime = DateTime.Now;
lastTimeForLimit = DateTime.Now;
int size = responseStream.Read(bArr, , (int)bArr.Length);
unitCount += size;
unitCountForLimit += size;
ms.Write(bArr, , size);
while (!isCompleted)
{
size = responseStream.Read(bArr, , (int)bArr.Length);
unitCount += size;
unitCountForLimit += size;
ms.Write(bArr, , size);
if (ms.Length > )
{
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("?") > )
{
return path + "\\" + fileName.Substring(, fileName.IndexOf("?"));
}
else
{
return path + "\\" + fileName;
}
}
#endregion } //end Form1类
}
测试截图:

C#限速下载网络文件的更多相关文章
- JAVA多线程下载网络文件
JAVA多线程下载网络文件,开启多个线程,同时下载网络文件. 源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...
- Java读取并下载网络文件
CreateTime--2017年8月21日10:11:07 Author:Marydon import java.io.ByteArrayOutputStream; import java.io ...
- python下载网络文件
python下载网络文件 制作人:全心全意 下载图片 #!/usr/bin/python #-*- coding: utf-8 -*- import requests url = "http ...
- DELPHI TDownLoadURL下载网络文件
DELPHI XE6 FMX 附件:http://files.cnblogs.com/xe2011/IDHttp_fmx.7z unit Unit1; interface uses //引用 Vc ...
- 【python】下载网络文件到本地
# 下载网络图片文件到本地 import urllib.request rsp=urllib.request.urlopen("http://n.sinaimg.cn/ent/transfo ...
- java 下载网络文件
1.FileUtils.copyURLToFile实现: import java.io.File; import java.net.URL; import org.apache.commons.io. ...
- python使用wget下载网络文件
wget是一个从网络上自动下载文件的自由工具.它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理. ubuntu 安装wget pip install wget 从网络或本地硬盘下载文件(并 ...
- 解决FTPClient下载网络文件线程挂起问题
今天在windows上调试FTP下载文件时,出险线程假死,代码如下: if (inputStream != null) { byte[] data = null; ByteArrayOutputStr ...
- 网络编程(一):用C#下载网络文件的2种方法
使用C#下载一个Internet上的文件主要是依靠HttpWebRequest/HttpWebResonse和WebClient.具体处理起来还有同步和异步两种方式,所以我们其实有四种组合. 1.使用 ...
随机推荐
- RIFF和WAVE音频文件格式
RIFF file format RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构.RIFF文 ...
- 手把手教你写一个RN小程序!
时间过得真快,眨眼已经快3年了! 1.我的第一个App 还记得我14年初写的第一个iOS小程序,当时是给别人写的一个单机的相册,也是我开发的第一个完整的app,虽然功能挺少,但是耐不住心中的激动啊,现 ...
- Missing Push Notification Entitlement 问题
最近打包上传是遇到一个问题: 描述: Missing Push Notification Entitlement - Your app includes an API for Apple's Push ...
- GSD_WeiXin(高仿微信)应用源码
高仿微信计划:已经实现功能 1.微信首页(cell侧滑编辑.下拉眼睛动画.下拉拍短视频.点击进入聊天详情界面) 2.通讯录(联系人字母排序.搜索界面) 3.发现(朋友圈) 4.我(界面) 待实现功能( ...
- linux系统下基于mono部署asp.net,使用ef6与mysql出现的问题【索引】
git clone github.com/mono的源码,日期:2014-06-19,百度网盘链接:http://pan.baidu.com/s/1kTG9EUb 关于asp.net利用mono部署到 ...
- ABP框架理论研究总结(典藏版)
目前,我已经完成了Module-Zero的翻译,请查看我的<Module-Zero学习目录>. 到现在为止,使用ABP框架开发正式项目已经3个月有余了,期间翻阅了大量文档资料,包括ABP官 ...
- Membership三步曲之进阶篇 - 深入剖析Provider Model
Membership 三步曲之进阶篇 - 深入剖析Provider Model 本文的目标是让每一个人都知道Provider Model 是什么,并且能灵活的在自己的项目中使用它. Membershi ...
- 快速开发Grunt插件----压缩js模板
前言 Grunt是一款前端构建工具,帮助我们自动化搭建前端工程.它可以实现自动对js.css.html文件的合并.压缩等一些列操作.Grunt有很多插件,每一款插件实现某个功能,你可以通过npm命名去 ...
- 每日设置Bing首页图片为壁纸
闲来无事,手痒痒要做一个什么小工具. 于是乎便有了本文. 当有一个想法的时候,首先免不了网上搜索一番以便看一下有木有网友有过类似的想法. 很显然--有! 因此本文大代码是从几个地方搜索,然后组合的. ...
- nfs 笔记 2
http://woxihuanpes.blog.163.com/blog/static/12423219820097139145238/ http://blog.csdn.net/willvc123/ ...