httpwebrequest hwr=(httpwebrequest)webrequest.create(http://www.163.com/); |
public system.windows.forms.listbox listbox1; private system.windows.forms.label label1; private system.windows.forms.textbox textbox1 private system.windows.forms.button button1; private system.windows.forms.label label2; private system.windows.forms.textbox textbox2; private system.windows.forms.label label3; private system.windows.forms.textbox textbox3; private system.windows.forms.label label4; 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;//进程数 |
public class httpfile { public form1 formm; public int threadh;//线程代号 public string filename;//文件名 public string strurl;//接收文件的url public filestream fs; public httpwebrequest request; public system.io.stream ns; public byte[] nbytes;//接收缓冲区 public int nreadsize;//接收字节数 public httpfile(form1 form,int thread)//构造方法 { formm=form; threadh=thread; } ~httpfile()//析构方法 { formm.dispose (); } public void receive()//接收线程 { filename=formm.filenamew[threadh]; strurl=formm.strurl; ns=null; nbytes= new byte[512]; nreadsize=0; formm.listbox1 .items .add ("线程"+threadh.tostring ()+"开始接收"); fs=new filestream (filename,system.io.filemode.create); try { request=(httpwebrequest)httpwebrequest.create (strurl); //接收的起始位置及接收的长度 request.addrange(formm.filestartw [threadh], formm.filestartw [threadh]+formm.filesizew [threadh]); ns=request.getresponse ().getresponsestream ();//获得接收流 nreadsize=ns.read (nbytes,0,512); while (nreadsize>0) { fs.write (nbytes,0,nreadsize); nreadsize=ns.read (nbytes,0,512); formm.listbox1 .items .add ("线程"+threadh.tostring ()+"正在接收"); } fs.close(); ns.close (); } catch (exception er) { messagebox.show (er.message ); fs.close(); } formm.listbox1 .items.add ("进程"+threadh.tostring ()+"接收完毕!"); formm.threadw[threadh]=true; } } |
private void button1_click(object sender, system.eventargs e) { datetime dt=datetime.now;//开始接收时间 textbox1.text =dt.tostring (); strurl=textbox2.text .trim ().tostring (); httpwebrequest request; long filesize=0; try { request=(httpwebrequest)httpwebrequest.create (strurl); filesize=request.getresponse ().contentlength;//取得目标文件的长度 request.abort (); } catch (exception er) { messagebox.show (er.message ); } // 接收线程数 thread=convert.toint32 (textbox4.text .trim().tostring (),10); //根据线程数初始化数组 threadw=new bool [thread]; filenamew=new string [thread]; filestartw=new int [thread]; filesizew=new int[thread]; //计算每个线程应该接收文件的大小 int filethread=(int)filesize/thread;//平均分配 int filethreade=filethread+(int)filesize%thread;//剩余部分由最后一个线程完成 //为数组赋值 for (int i=0;i<thread;i++) { threadw[i]=false;//每个线程状态的初始值为假 filenamew[i]=i.tostring ()+".dat";//每个线程接收文件的临时文件名 if (i<thread-1) { filestartw[i]=filethread*i;//每个线程接收文件的起始点 filesizew[i]=filethread-1;//每个线程接收文件的长度 } else { filestartw[i]=filethread*i; filesizew[i]=filethreade-1; } } //定义线程数组,启动接收线程 thread[] threadk=new thread [thread]; httpfile[] httpfile=new httpfile [thread]; for (int j=0;j<thread;j++) { httpfile[j]=new httpfile(this,j); threadk[j]=new thread(new threadstart (httpfile[j].receive )); threadk[j].start (); } //启动合并各线程接收的文件线程 thread hbth=new thread (new threadstart (hbfile)); hbth.start (); } |
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;k<thread;k++) { fstemp=new filestream (filenamew[k],system.io.filemode .open); while (true) { readfile=fstemp.read (bytes,0,512); if (readfile>0) { fs.write (bytes,0,readfile); } else { break; } } fstemp.close (); } fs.close (); datetime dt=datetime.now; textbox1.text =dt.tostring ();//结束时间 messagebox.show ("接收完毕!!!"); } |
新闻热点
疑难解答