using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Threading;
namespace HttpDownTest
{
public partial class Form1 : Form
{
public Form1()//初始化,自动生成的
{
InitializeComponent();
}
private String save_path = "";//保存路径
private String download_path = "";//下载路径
private String type = "";
private Thread thread;//线程
private int pause = 0;//暂停变量
private int stop = 0;//停止变量
private int OKOK = 0;
private void button1_Click(object sender, EventArgs e)//主要函数
{
OKOK = 1;
if (pause == 1)
{
textBox2.Text = "暂停中!";
}
else
{
thread = new Thread(start);//线程对象,下句是线程开始
thread.Start();
}
}
private void start()//开始函数
{
if (textBox1.Text == "")
{
textBox2.Text = "下载地址为空!";
return;
}
if (textBox3.Text == "")
{
textBox2.Text = "保存地址为空!";
textBox3.Text = "C:\\Users\\dl201507213\\Desktop\\";
return;
}
if (textBox5.Text == "")
{
textBox2.Text = "保存名字为空!";
textBox5.Text = "test";
return;
}
if (textBox6.Text == "")
{
textBox2.Text = "保存类型为空!";
textBox6.Text = "ppt";
return;
}
if (radioButton1.Checked)
{
type = ".jpg";
}
else if (radioButton2.Checked)
{
type = ".docx";
}
else if (radioButton3.Checked)
{
type = ".txt";
}
else
{
type = "." + textBox6.Text;
}
save_path = textBox3.Text + textBox5.Text + type;
download_path = textBox1.Text;
textBox2.Text = "";//上面三句是路径设置
System.Net.HttpWebRequest httpWebRequest = null;
try
{
//url获取远程文件流。。。
httpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(download_path);
System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
System.IO.Stream sr = httpWebResponse.GetResponseStream();
//创建本地文件写入流。。。
System.IO.Stream sw = new System.IO.FileStream(save_path, System.IO.FileMode.Create);
long totalDownloadedByte = 0;
byte[] by = new byte[102400];//每次读入100K
Stopwatch watch = new Stopwatch();
watch.Start();//上面两句是用来算时间的
int osize = sr.Read(by, 0, (int)by.Length);
while ((osize > 0) && (stop == 0))
{
while ((osize > 0) && (pause == 0) && (stop == 0))
{
textBox7.Text = Convert.ToString(totalDownloadedByte/1024);
totalDownloadedByte = osize + totalDownloadedByte;
sw.Write(by, 0, osize);//下载
osize = sr.Read(by, 0, (int)by.Length);//记取内容缓冲
System.Threading.Thread.Sleep(1);
textBox4.Text = (Convert.ToString(osize/1024) + " KB/s");
//速度显示
}
System.Threading.Thread.Sleep(100);
}
watch.Stop();
textBox8.Text = Convert.ToString(watch.ElapsedMilliseconds/1000)+ " s";//算出耗时
sw.Close();
sr.Close();
if (stop == 1)
{
stop = 0;
pause = 0;
textBox2.Text = "停止下载!";
button4.Text = "暂停";
}
else
{
textBox2.Text = "下载成功!";
}
}
catch (System.Exception)
{
if (httpWebRequest != null)
httpWebRequest.Abort();
textBox2.Text = "下载失败!";
thread.Abort();
}
thread.Abort();//关闭线程
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
download_path = textBox1.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
DateTime current_time = new DateTime();
current_time = DateTime.Now;
label3.Text = Convert.ToString(current_time);//获得当前系统时间
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog select_save_path = new FolderBrowserDialog();
select_save_path.ShowNewFolderButton = true;//这个用于是否可以新建文件夹
select_save_path.Description = "请选择保存路径";
select_save_path.ShowDialog();//用默认的对话框。。。
textBox3.Text = select_save_path.SelectedPath + "\\";
}
private void button4_Click(object sender, EventArgs e)
{
if (pause == 1)
{
pause = 0;
button4.Text = "暂停";
}
else if (pause == 0)
{
button4.Text = "继续";
pause = 1;
}
return;
}
private void button3_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();//显示作者信息
}
private void button5_Click(object sender, EventArgs e)
{
if (stop == 0) { stop = 1; }
else { stop = 0; button5.Text = "停止下载"; }
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (OKOK == 1)
{
thread.Abort();
Application.Exit();//关闭右上角的X的时候的处理办法,关闭所以线程
}
}
}
}
二、界面2设计与代码