关于文件的切割下载
1 public class Program 2 { 3 public static CountdownEvent cde = new CountdownEvent(0); 4 5 //每个线程下载的字节数,方便最后合并 6 public static ConcurrentDictionary<long, byte[]> dic = new ConcurrentDictionary<long, byte[]>(); 7 8 //请求文件 9 public static string url = "http://www.pncity.net/bbs/data/attachment/forum/201107/30/1901108yyd8gnrs2isadrr.jpg"; 10 11 static void Main(string[] args) 12 { 13 for (int i = 0; i < 5; i++) 14 { 15 Console.WriteLine("\n****************************\n第{0}次比较\n****************************", (i + 1)); 16 17 //不用线程 18 //RunSingle(); 19 20 //使用多线程 21 RunMultiTask(); 22 } 23 24 Console.Read(); 25 } 26 27 static void RunMultiTask() 28 { 29 Stopwatch watch = Stopwatch.StartNew(); 30 31 //开5个线程 32 int threadCount = 5; 33 34 long start = 0; 35 36 long end = 0; 37 38 var total = GetSourceHead(); 39 40 if (total == 0) 41 return; 42 43 var pageSize = (int)Math.Ceiling((Double)total / threadCount); 44 45 // cde.Reset(threadCount); 46 47 Task[] tasks = new Task[threadCount]; 48 49 for (int i = 0; i < threadCount; i++) 50 { 51 start = i * pageSize; 52 53 end = (i + 1) * pageSize - 1; 54 55 if (end > total) 56 end = total; 57 58 var obj = start + "|" + end; 59 60 tasks[i] = Task.Factory.StartNew(j => new DownFile().DownTaskMulti(obj), obj); 61 } 62 63 Task.WaitAll(tasks); 64 65 var targetFile = "C://down//" + url.Substring(url.LastIndexOf('/') + 1); 66 67 FileStream fs = new FileStream(targetFile, FileMode.Create); 68 69 var result = dic.Keys.OrderBy(i => i).ToList(); 70 71 foreach (var item in result) 72 { 73 fs.Write(dic[item], 0, dic[item].Length); 74 } 75 76 fs.Close(); 77 78 watch.Stop(); 79 80 Console.WriteLine("多线程:下载耗费时间:{0}", watch.Elapsed); 81 } 82 83 static void RunSingle() 84 { 85 Stopwatch watch = Stopwatch.StartNew(); 86 87 if (GetSourceHead() == 0) 88 return; 89 90 var request = (HttpWebRequest)HttpWebRequest.Create(url); 91 92 var response = (HttpWebResponse)request.GetResponse(); 93 94 var stream = response.GetResponseStream(); 95 96 var outStream = new MemoryStream(); 97 98 var bytes = new byte[10240]; 99 100 int count = 0; 101 102 while ((count = stream.Read(bytes, 0, bytes.Length)) != 0) 103 { 104 outStream.Write(bytes, 0, count); 105 } 106 107 var targetFile = "C://down//" + url.Substring(url.LastIndexOf('/') + 1); 108 109 FileStream fs = new FileStream(targetFile, FileMode.Create); 110 111 fs.Write(outStream.ToArray(), 0, (int)outStream.Length); 112 113 outStream.Close(); 114 115 response.Close(); 116 117 fs.Close(); 118 119 watch.Stop(); 120 121 Console.WriteLine("不用线程:下载耗费时间:{0}", watch.Elapsed); 122 } 123 124 //获取头信息 125 public static long GetSourceHead() 126 { 127 var request = (HttpWebRequest)HttpWebRequest.Create(url); 128 129 request.Method = "Head"; 130 request.Timeout = 3000; 131 132 var response = (HttpWebResponse)request.GetResponse(); 133 134 var code = response.StatusCode; 135 136 if (code != HttpStatusCode.OK) 137 { 138 Console.WriteLine("下载的资源无效!"); 139 return 0; 140 } 141 142 var total = response.ContentLength; 143 144 Console.WriteLine("当前资源大小为:" + total); 145 146 response.Close(); 147 148 return total; 149 } 150 } 151 152 public class DownFile 153 { 154 // 多线程下载 155 public void DownTaskMulti(object obj) 156 { 157 var single = obj.ToString().Split('|'); 158 159 long start = Convert.ToInt64(single.FirstOrDefault()); 160 161 long end = Convert.ToInt64(single.LastOrDefault()); 162 163 var request = (HttpWebRequest)HttpWebRequest.Create(Program.url); 164 165 request.AddRange(start, end); 166 167 var response = (HttpWebResponse)request.GetResponse(); 168 169 var stream = response.GetResponseStream(); 170 171 var outStream = new MemoryStream(); 172 173 var bytes = new byte[10240]; 174 175 int count = 0; 176 177 while ((count = stream.Read(bytes, 0, bytes.Length)) != 0) 178 { 179 outStream.Write(bytes, 0, count); 180 } 181 182 outStream.Close(); 183 184 response.Close(); 185 186 Program.dic.TryAdd(start, outStream.ToArray()); 187 188 // Program.cde.Signal(); 189 } 190 }