在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
用C#实现HTTP协议下的多线程文件传输 1 public System.Windows.Forms.ListBox listBox1; 2 private System.Windows.Forms.Label label1; 3 private System.Windows.Forms.TextBox textBox1 4 private System.Windows.Forms.Button button1; 5 private System.Windows.Forms.Label label2; 6 private System.Windows.Forms.TextBox textBox2; 7 private System.Windows.Forms.Label label3; 8 private System.Windows.Forms.TextBox textBox3; 9 private System.Windows.Forms.Label label4; 10 private System.Windows.Forms.TextBox textBox4;
using System.Net;//网络功能 using System.IO;//流支持 using System.Threading ;//线程支持 增加如下的程序变量: public bool[] threadw; //每个线程结束标志 public string[] filenamew;//每个线程接收文件的文件名 public int[] filestartw;//每个线程接收文件的起始位置 public int[] filesizew;//每个线程接收文件的大小 public string strurl;//接受文件的URL public bool hb;//文件合并标志 public int thread;//进程数
1 public class HttpFile 2 { 3 public Form1 formm; 4 public int threadh;//线程代号 5 public string filename;//文件名 6 public string strUrl;//接收文件的URL 7 public FileStream fs; 8 public HttpWebRequest request; 9 public System.IO.Stream ns; 10 public byte[] nbytes;//接收缓冲区 11 public int nreadsize;//接收字节数 12 public HttpFile(Form1 form,int thread)//构造方法 13 { 14 formm=form; 15 threadh=thread; 16 } 17 ~HttpFile()//析构方法 18 { 19 formm.Dispose (); 20 } 21 public void receive()//接收线程 22 { 23 filename=formm.filenamew[threadh]; 24 strUrl=formm.strurl; 25 ns=null; 26 nbytes= new byte[512]; 27 nreadsize=0; 28 formm.listBox1 .Items .Add ("线程"+threadh.ToString ()+"开始接收"); 29 fs=new FileStream (filename,System.IO.FileMode.Create); 30 try 31 { 32 request=(HttpWebRequest)HttpWebRequest.Create (strUrl); 33 //接收的起始位置及接收的长度 34 request.AddRange(formm.filestartw [threadh], 35 formm.filestartw [threadh]+formm.filesizew [threadh]); 36 ns=request.GetResponse ().GetResponseStream ();//获得接收流 37 nreadsize=ns.Read (nbytes,0,512); 38 while (nreadsize>0) 39 { 40 fs.Write (nbytes,0,nreadsize); 41 nreadsize=ns.Read (nbytes,0,512); 42 formm.listBox1 .Items .Add ("线程"+threadh.ToString ()+"正在接收"); 43 } 44 fs.Close(); 45 ns.Close (); 46 } 47 catch (Exception er) 48 { 49 MessageBox.Show (er.Message ); 50 fs.Close(); 51 } 52 formm.listBox1 .Items.Add ("进程"+threadh.ToString ()+"接收完毕!"); 53 formm.threadw[threadh]=true; 54 }} 55 56 该类和Form1类处于统一命名空间,但不包含在Form1类中。下面定义“开始接收”按钮控件的事件响应函数: 57 58 private void button1_Click(object sender, System.EventArgs e) 59 { 60 DateTime dt=DateTime.Now;//开始接收时间 61 textBox1.Text =dt.ToString (); 62 strurl=textBox2.Text .Trim ().ToString (); 63 HttpWebRequest request; 64 long filesize=0; 65 try 66 { 67 request=(HttpWebRequest)HttpWebRequest.Create (strurl); 68 filesize=request.GetResponse ().ContentLength;//取得目标文件的长度 69 request.Abort (); 70 } 71 catch (Exception er) 72 { 73 MessageBox.Show (er.Message ); 74 } 75 // 接收线程数 76 thread=Convert.ToInt32 (textBox4.Text .Trim().ToString (),10); 77 //根据线程数初始化数组 78 threadw=new bool [thread]; 79 filenamew=new string [thread]; 80 filestartw=new int [thread]; 81 filesizew=new int[thread]; 82 83 //计算每个线程应该接收文件的大小 84 int filethread=(int)filesize/thread;//平均分配 85 int filethreade=filethread+(int)filesize%thread;//剩余部分由最后一个线程完成 86 //为数组赋值 87 for (int i=0;i<thread;i++) 88 { 89 threadw[i]=false;//每个线程状态的初始值为假 90 filenamew[i]=i.ToString ()+".dat";//每个线程接收文件的临时文件名 91 if (i<thread-1) 92 { 93 filestartw[i]=filethread*i;//每个线程接收文件的起始点 94 filesizew[i]=filethread-1;//每个线程接收文件的长度 95 } 96 else 97 { 98 filestartw[i]=filethread*i; 99 filesizew[i]=filethreade-1; 100 } 101 } 102 //定义线程数组,启动接收线程 103 Thread[] threadk=new Thread [thread]; 104 HttpFile[] httpfile=new HttpFile [thread]; 105 for (int j=0;j<thread;j++) 106 { 107 httpfile[j]=new HttpFile(this,j); 108 threadk[j]=new Thread(new ThreadStart (httpfile[j].receive )); 109 threadk[j].Start (); 110 } 111 //启动合并各线程接收的文件线程 112 Thread hbth=new Thread (new ThreadStart (hbfile)); 113 hbth.Start (); 114 }
public void hbfile() { while (true)//等待 { hb=true; for (int i=0;i<thread;i++) { if (threadw[i]==false)//有未结束线程,等待 { hb=false; Thread.Sleep (100); break; } } if (hb==true)//所有线程均已结束,停止等待, { break; } } FileStream fs;//开始合并 FileStream fstemp; int readfile; byte[] bytes=new byte[512]; fs=new FileStream (textBox3.Text .Trim ().ToString (),System.IO.FileMode.Create); for (int k=0;k0) { fs.Write (bytes,0,readfile); } else { break; } } fstemp.Close (); } fs.Close (); DateTime dt=DateTime.Now; textBox1.Text =dt.ToString ();//结束时间 MessageBox.Show ("接收完毕!!!"); }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论