예제 #1
0
        public void Start_load(int number,string url,string path1)
        {
            downornot = true;
            strurl = url;
            GetPath(path1);
            GetURLType(url);
            GetFileName(url);
            setfilename = path+true_filename + "set." + "txt";
            if (File.Exists(setfilename))
            {
                Console.WriteLine("exist");
                Restart_load();
            }
            else
            {
                Console.WriteLine("not exist");
                dt = DateTime.Now;//开始接收时间
                HttpWebRequest request;
                long filesize = 0;
                downloadcontrol downcontrol = new downloadcontrol(this);   //set speed etc.
                testspeed = new Thread(downcontrol.set);
                testspeed.Start();
                try
                {
                    request = (HttpWebRequest)HttpWebRequest.Create(strurl);
                    filesize = request.GetResponse().ContentLength;//取得目标文件的长度
                    DownLoadsize = filesize;
                    request.Abort();
                }
                catch (Exception er)
                {
                    //MessageBox.Show (er.Message );
                }
                // 接收线程数
                thread = number;
                //根据线程数初始化数组
                threadw = new bool[thread];
                filenamew = new string[thread];
                filestartw = new long[thread];
                filesizew = new long[thread];
                //计算每个线程应该接收文件的大小
                long filethread = filesize / thread;//平均分配
                long filethreade = filesize - filethread * (thread - 1);//剩余部分由最后一个线程完成
                //为数组赋值
                for (int i = 0; i < thread; i++)
                {
                    threadw[i] = false;//每个线程状态的初始值为假
                    filenamew[i] = path+true_filename + i.ToString() + ".dat";//每个线程接收文件的临时文件名
                    if (i < thread - 1)
                    {
                        filestartw[i] = filethread * i;//每个线程接收文件的起始点
                        filesizew[i] = filethread - 1;//每个线程接收文件的长度
                    }
                    else
                    {
                        filestartw[i] = filethread * i;
                        filesizew[i] = filethreade - 1;
                    }

                }
                //保存到文件
                FileStream setfs;
                setfs = new FileStream(path+true_filename + "set." + "txt", System.IO.FileMode.Create);
                StreamWriter sw = new StreamWriter(setfs);
                sw.WriteLine(strurl);
                sw.WriteLine(thread);
                sw.WriteLine(path);
                for (int i = 0; i < thread; i++)
                {
                    sw.WriteLine(filenamew[i]);//每个线程接收文件的文件名
                    sw.WriteLine(filestartw[i]);//每个线程接收文件的起始位置
                    sw.WriteLine(filesizew[i]);//每个线程接收文件的大小
                    sw.WriteLine(threadw[i]);
                }
                sw.WriteLine(dt);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                setfs.Close();
                //定义线程数组,启动接收线程
                threadk = new Thread[thread];
                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();
                }
                //启动合并各线程接收的文件线程
                hbth = new Thread(new ThreadStart(hbfile));
                hbth.Start();
            }
        }
예제 #2
0
 public void Restart_load()
 {
     HttpWebRequest request;
     request = (HttpWebRequest)HttpWebRequest.Create(strurl);
     DownLoadsize= request.GetResponse().ContentLength;//取得目标文件的长度
     request.Abort();
     StreamReader sw = new StreamReader(path+true_filename + "set." + "txt");
     strurl=sw.ReadLine();
     thread=Convert.ToInt32(sw.ReadLine());
     path = sw.ReadLine();
     //根据线程数初始化数组
     threadw = new bool[thread];
     filenamew = new string[thread];
     filestartw = new long[thread];
     filesizew = new long[thread];
     for (int i = 0; i < thread; i++)
     {
         filenamew[i]=sw.ReadLine();//每个线程接收文件的文件名
         filestartw[i]=Convert.ToInt32(sw.ReadLine());//每个线程接收文件的起始位置
         filesizew[i] = Convert.ToInt32(sw.ReadLine());//每个线程接收文件的大小
         threadw[i] = Convert.ToBoolean(sw.ReadLine());
         Console.WriteLine(filenamew[i]+filesizew[i].ToString());
     }
     dt = Convert.ToDateTime(sw.ReadLine());
     sw.Close();
     Console.ReadLine();
     threadk = new Thread[thread];
     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();
     }
     downloadcontrol downcontrol = new downloadcontrol(this);
     testspeed = new Thread(downcontrol.set);
     testspeed.Start();
     //启动合并各线程接收的文件线程
     hbth = new Thread(new ThreadStart(hbfile));
     hbth.Start();
 }