用C#语言实现http协议下的多线程文件传输(代码)(转)

Posted on 2007-05-24 21:35  Leon0812  阅读(446)  评论(0)    收藏  举报
  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7using System.Net; 
  8using System.IO;
  9using System.Threading ;
 10namespace httpftp
 11{
 12 /// <summary>
 13 /// Form1 的摘要说明。
 14 /// </summary>

 15 public class Form1 : System.Windows.Forms.Form
 16 {
 17  public  System.Windows.Forms.ListBox listBox1;
 18  private System.Windows.Forms.Button button1;
 19  private System.Windows.Forms.Label label2;
 20  private System.Windows.Forms.TextBox textBox2;
 21  private System.Windows.Forms.Label label3;
 22  private System.Windows.Forms.TextBox textBox3;
 23  private System.Windows.Forms.Label label4;
 24  private System.Windows.Forms.TextBox textBox4;
 25  public bool[] threadw; //每个进程结束标志
 26  public string[] filenamew;//每个进程接收文件的文件名
 27  public int[] filestartw;//每个进程接收文件的起始位置
 28  public int[] filesizew;//每个进程接收文件的大小
 29  public string strurl;//接受文件的URL
 30  public bool hb;//文件合并标志
 31  public int thread;//进程数
 32  private System.Windows.Forms.Label label1;
 33  private System.Windows.Forms.TextBox textBox1;
 34  /// <summary>
 35  /// 必需的设计器变量。
 36  /// </summary>

 37  private System.ComponentModel.Container components = null;
 38  public Form1()
 39  {
 40   //
 41   // Windows 窗体设计器支持所必需的
 42   //
 43   InitializeComponent();
 44   //
 45   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 46   //
 47  }

 48  /// <summary>
 49  /// 清理所有正在使用的资源。
 50  /// </summary>

 51  protected override void Dispose( bool disposing )
 52  {
 53   if( disposing )
 54   {
 55    if (components != null
 56    {
 57     components.Dispose();
 58    }

 59   }

 60   base.Dispose( disposing );
 61  }

 62  Windows Form Designer generated code
181  /// <summary>
182  /// 应用程序的主入口点。
183  /// </summary>

184  [STAThread]
185  static void Main() 
186  {
187   Application.Run(new Form1());
188  }

189  private void button1_Click(object sender, System.EventArgs e)
190  {
191   DateTime dt=DateTime.Now;
192   textBox1.Text =dt.ToString ();
193   strurl=textBox2.Text .Trim ().ToString ();
194   HttpWebRequest request;
195   long filesize=0;
196   try
197   {
198    request=(HttpWebRequest)HttpWebRequest.Create (strurl);
199    filesize=request.GetResponse ().ContentLength;//取得目标文件的长度
200    request.Abort ();
201   }

202   catch (Exception er)
203   {
204    MessageBox.Show (er.Message );
205   }

206   // 接收线程数
207   thread=Convert.ToInt32 (textBox4.Text .Trim().ToString (),10);
208   //初始化数组
209   threadw=new bool [thread];
210   filenamew=new string [thread];
211   filestartw=new int [thread];
212   filesizew=new int[thread];
213   
214   //计算每个进程接收文件的大小
215   int filethread=(int)filesize/thread;
216   int filethreade=filethread+(int)filesize%thread;
217   //为数组赋值
218   for (int i=0;i<thread;i++)
219   {
220    threadw[i]=false;
221    filenamew[i]=i.ToString ()+".dat";
222    if (i<thread-1)
223    {
224     filestartw[i]=filethread*i;
225     filesizew[i]=filethread-1;
226     
227    }

228    else
229    {
230     filestartw[i]=filethread*i;
231     filesizew[i]=filethreade-1;
232    }

233   }

234   //定义线程数组
235   Thread[] threadk=new Thread [thread];
236   HttpFile[] httpfile=new HttpFile [thread];
237   for (int j=0;j<thread;j++)
238   {
239    httpfile[j]=new HttpFile(this,j);
240    threadk[j]=new Thread(new ThreadStart (httpfile[j].receive ));
241    threadk[j].Start ();
242   }

243   //合并各线程接收的文件
244   Thread hbth=new Thread (new ThreadStart (hbfile));
245   hbth.Start ();
246  }

247  //合并文件线程
248  public void hbfile()
249  {
250   while (true)//等待
251   {
252    hb=true;
253    for (int i=0;i<thread;i++)
254    {
255     if (threadw[i]==false)
256     {
257      hb=false;
258      Thread.Sleep (100);
259      break;
260     }

261    }

262    if (hb==true)
263    {
264     break;
265    }

266   }

267   FileStream fs;//开始合并
268   FileStream fstemp;
269   int readfile;
270   byte[] bytes=new byte[512];
271   fs=new FileStream (textBox3.Text .Trim ().ToString (),System.IO.FileMode.Create);
272   for (int k=0;k<thread;k++)
273   {
274    fstemp=new FileStream (filenamew[k],System.IO.FileMode .Open);
275    while (true)
276    {
277     readfile=fstemp.Read (bytes,0,512);
278     if (readfile>0)
279     {
280      fs.Write (bytes,0,readfile);
281     }

282     else
283     {
284      break;
285     }

286    }

287    fstemp.Close ();
288   }

289   fs.Close ();
290   DateTime dt=DateTime.Now;
291   textBox1.Text =dt.ToString ();
292   MessageBox.Show ("接收完毕!!!");
293  }

294 }

295 //接收线程类 
296 public class HttpFile
297 {
298  public Form1 formm;
299  public int threadh;
300  public string filename;
301  public string strUrl;
302  public FileStream fs;
303  public HttpWebRequest request;
304  public System.IO.Stream ns;
305  public byte[] nbytes;
306  public int nreadsize;
307  public HttpFile(Form1 form,int thread)//构造方法
308  {
309   formm=form;
310   threadh=thread;
311  }

312   ~HttpFile()//析构方法
313  {
314    formm.Dispose ();
315  }

316  public void receive()
317  {
318   filename=formm.filenamew[threadh];
319   strUrl=formm.strurl;
320   ns=null;
321   nbytes= new byte[512];
322   nreadsize=0;
323   formm.listBox1 .Items .Add ("线程"+threadh.ToString ()+"开始接收");
324   fs=new FileStream (filename,System.IO.FileMode.Create);
325   try
326   {
327    request=(HttpWebRequest)HttpWebRequest.Create (strUrl);
328    request.AddRange(formm.filestartw [threadh],formm.filestartw [threadh]+formm.filesizew [threadh]);
329    ns=request.GetResponse ().GetResponseStream ();
330    nreadsize=ns.Read (nbytes,0,512);
331    while (nreadsize>0)
332    {
333     fs.Write (nbytes,0,nreadsize);
334     nreadsize=ns.Read (nbytes,0,512);
335     formm.listBox1 .Items .Add ("线程"+threadh.ToString ()+"正在接收");
336    }

337    fs.Close();
338    ns.Close ();
339   }

340   catch (Exception er)
341   {
342    MessageBox.Show (er.Message );
343    fs.Close();
344   }

345   formm.listBox1 .Items.Add ("进程"+threadh.ToString ()+"接收完毕!");
346   formm.threadw[threadh]=true;
347  }

348 }

349}

350

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3