private void btn_Start_Click(object sender, EventArgs e) { var file = cmb_ChooseExcel.Text; string path = txt_FilePath.Text; if (string.IsNullOrEmpty(file)) { MessageBox.Show(this, "请选择excel文件", "提示"); return; } if (string.IsNullOrEmpty(path)) { MessageBox.Show(this, "请选择文件所在路径", "提示"); return; } string filePath = Path.Combine(path, file); List <TrafficGroupParam> paramList = new List <TrafficGroupParam>(); try { DataSet ds = CreateDownloadInfo(filePath); if (ds != null) { richTextBox1.Text += "开始读取文件内容...\r\n"; var dsInfo = ds.Tables["车辆轨迹明细数据"]; Dictionary <string, List <string> > dictUrls = new Dictionary <string, List <string> >(); TrafficGroupParam param = null; string picPath = string.Empty, strSpottingNamne = string.Empty, strDirectionName = string.Empty, groupKey = string.Empty, plateNo = string.Empty, platecolor = string.Empty; DateTime?passTime = null; foreach (DataRow dr in dsInfo.Rows) { if (dr["链接"] != null && dr["链接"] != DBNull.Value) { picPath = dr["链接"].ToString(); if (dr["经过路口"] != null) { strSpottingNamne = dr["经过路口"].ToString(); } if (dr["经过时间"] != null) { passTime = Convert.ToDateTime(dr["经过时间"]); } if (dr["号牌号码"] != null) { plateNo = dr["号牌号码"].ToString(); } if (dr["号牌颜色"] != null) { platecolor = dr["号牌颜色"].ToString(); } param = new TrafficGroupParam { Path = picPath, SpottingName = strSpottingNamne, PlateNo = plateNo + " " + platecolor, DirectionName = strDirectionName, PassingTime = passTime, SavePath = txt_ImagePath.Text, PassingFileName = $"{passTime.Value.ToString("yyyy-MM-dd HH-mm-ss")} {plateNo}" }; paramList.Add(param); } } Handler handle = new Handler(); handle.OnStart += (s, ev) => { this.Invoke(new MethodInvoker(() => { richTextBox1.Text += "开始下载图片,图片地址:" + ev.Url + "\r\n"; }), s); }; handle.OnError += (s, ev) => { this.Invoke(new MethodInvoker(() => { richTextBox1.Text += $"{ev.Exception.ToString()}【下载出错】,图片:{ev.Uri}未能下载成功,\r\n"; }), s); }; handle.OnCompleted += (s, ev) => { this.Invoke(new MethodInvoker(() => { richTextBox1.Text += $"{ev.Uri}【下载完成】\r\n"; }), s); }; handle.Execute(paramList); paramList.Clear(); MessageBox.Show("下载完成"); } else { return; } } catch { } }
/// <summary> /// 下载方法(读取传入对象) /// </summary> /// <param name="paramList"></param> /// <param name="success"></param> /// <param name="fail"></param> public void Execute(List <TrafficGroupParam> paramList) { var fileInfo = paramList.GroupBy(e => e.PlateNo).Select(e => new GroupFileInfo { FileName = e.Key, TrafficGroupParamList = e.ToList() }).ToList(); string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; if (paramList.Count < 1) { throw new Exception("没有要导出的文件!"); } foreach (var item in fileInfo) { string tempPath = string.IsNullOrEmpty(item.TrafficGroupParamList[0].SavePath) == true ? baseDirectory : item.TrafficGroupParamList[0].SavePath; tempPath = Path.Combine(tempPath, item.FileName); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } //如果小于或等于容器大小.则无需开启线程编译 DownLoadParams downloadParam = null; var pathsCount = item.TrafficGroupParamList.Count;//图片数量,用于判断是否启用多线程 if (pathsCount <= Number100) { downloadParam = new DownLoadParams() { TrafficGroupParam = item.TrafficGroupParamList.ToArray() }; DownLoadDork(downloadParam); } else { // 计算编译线程数量 int threadCount = pathsCount % Number100 == 0 ? pathsCount / Number100 : pathsCount / Number100 + 1; if (threadCount > MaxThreadCount) { threadCount = MaxThreadCount; } int threadPqgeSize = (pathsCount / threadCount) + 1; int typeSum = 0; // 为每个线程准备调用参数 List <DownLoadParams> parameters = new List <DownLoadParams>(threadCount); TrafficGroupParam[] trafficGroupParam = null; int index, endSize = 0;; for (int i = 0; i < threadCount; i++) { downloadParam = new DownLoadParams(); endSize = threadPqgeSize * (i + 1); if (endSize > pathsCount) { trafficGroupParam = new TrafficGroupParam[threadPqgeSize + pathsCount - endSize]; endSize = pathsCount; } else { trafficGroupParam = new TrafficGroupParam[threadPqgeSize]; } index = 0; for (int j = typeSum; j < endSize; j++) { trafficGroupParam[index++] = item.TrafficGroupParamList[j]; } downloadParam.TrafficGroupParam = trafficGroupParam; parameters.Add(downloadParam); } // 创建编译线程 List <Thread> threads = new List <Thread>(threadCount); for (int i = 1; i < threadCount; i++) { Thread thread = new Thread(DownLoadDork); thread.IsBackground = true; thread.Name = "DownloadThread #" + i.ToString(); threads.Add(thread); thread.Start(parameters[i]); } // 重用当前线程:为当前线程指派下载任务。 DownLoadDork(parameters[0]); // 等待所有的下载线程执行线束。 foreach (Thread thread in threads) { thread.Join(); } } } //success = tempSuccess; //fail = tempFail; }
public event EventHandler <OnErrorEventArgs> OnError; //出错事件 /// <summary> /// 下载图片 /// </summary> /// <param name="param">文件信息对象</param> /// <returns></returns> private async void Download(TrafficGroupParam param) { await Task.Run(() => { string filePath = param.Path; string savePath = $"{AppDomain.CurrentDomain.BaseDirectory}/{param.PlateNo}"; savePath = string.IsNullOrEmpty(param.SavePath) == true ? savePath : $"{param.SavePath}/{param.PlateNo}"; //取文件名后缀 string fileName = Path.GetFileName(filePath); var fileSuffix = fileName.Split('.'); var arr = filePath.Split(new string[] { " || " }, StringSplitOptions.RemoveEmptyEntries); string localPath = string.Empty; if (arr.Length == 2 && !string.IsNullOrEmpty(arr[1])) { filePath = arr[0]; localPath = Path.Combine(savePath, arr[1]); } else { string extName = fileSuffix.Contains("jpg") && fileSuffix.Contains("png") == true ? fileSuffix.FirstOrDefault(e => e.Contains("jpg") && e.Contains("png")) : "jpg"; localPath = Path.Combine(savePath, $"{param.PassingFileName}.{extName}"); } if (Regex.IsMatch(filePath, @"^\\{2}", RegexOptions.IgnoreCase)) { System.IO.File.Copy(filePath, localPath, true); return; } if (Regex.IsMatch(filePath, @"^[a-zA-Z]:\\", RegexOptions.IgnoreCase)) { System.IO.File.Copy(filePath, localPath, true); return; } if (Regex.IsMatch(filePath, "^ftp://", RegexOptions.IgnoreCase)) { try { new FtpClient().Get(filePath, savePath, Path.GetFileName(filePath)); } catch (Exception ex) { if (this.OnError != null) { this.OnError(this, new OnErrorEventArgs(filePath, ex)); } } return; } if (!Regex.IsMatch(filePath, "^http://", RegexOptions.IgnoreCase) && !Regex.IsMatch(filePath, "^https://", RegexOptions.IgnoreCase)) { return; } ToggleAllowUnsafeHeaderParsing(true); try { if (this.OnStart != null) { OnStart(this, new OnStartEventArgs(filePath)); } var watch = new Stopwatch(); watch.Start(); using (WebResponse response = WebRequest.Create(filePath).GetResponse()) { try { using (Stream stream = response.GetResponseStream()) { byte[] buffer = new byte[0x400]; int count = 0; using (FileStream stream2 = new FileStream(localPath, FileMode.Create, FileAccess.Write)) { while ((count = stream.Read(buffer, 0, 0x400)) > 0) { stream2.Write(buffer, 0, count); stream2.Flush(); } } } } catch (WebException ex) { var res = (WebResponse)ex.Response; if (res != null) { StreamReader sr = new StreamReader(res.GetResponseStream()); var strHtml = sr.ReadToEnd(); WriteLog($"请求地址:{filePath}" + strHtml, "errorInfo"); } else { WriteLog($"请求地址:{filePath}" + ex.ToString(), "errorInfo"); } } } watch.Stop(); var threadId = System.Threading.Thread.CurrentThread.ManagedThreadId; //获取当前任务线程ID var milliseconds = watch.ElapsedMilliseconds; //获取请求执行时间 if (this.OnCompleted != null) { this.OnCompleted(this, new OnCompletedEventArgs(filePath, threadId, milliseconds)); } } catch (WebException ex) { if (this.OnError != null) { this.OnError(this, new OnErrorEventArgs(filePath, ex)); } } }); }
/// <summary> /// 获取图片本地存储路径 /// </summary> /// <param name="path">图片http路径</param> /// <param name="DirectionName">方向名称</param> /// <param name="SpottingName">路口名称</param> ///<param name="dictUrls">输出参数 存储当前数据分组后每个组的图片信息</param> /// <returns>下载完成后的本地路径</returns> public static string GetPicLocalPath(TrafficGroupParam param, Dictionary <string, List <string> > dictUrls) { string localPath = string.Empty; if (dictUrls == null) { dictUrls = new Dictionary <string, List <string> >(); } if (!string.IsNullOrEmpty(param.Path)) { string fileName = Path.GetFileName(param.Path); var ext = Path.GetExtension(param.Path).ToLower(); //判断是否为图片文件路径 bool picPath = true; if (!ext.Contains(".jpg") && !ext.Contains(".png") && !ext.Contains(".bmp") && !ext.Contains(".gif")) { fileName = Guid.NewGuid().ToString("N") + ".jpg"; picPath = false; } string groupKey = string.Empty; List <string> realUrls = null; string PicGroupType = "5";//TrafficExportConfig.PicGroupType; switch (PicGroupType) { case "0": localPath = "导出图片\\" + fileName; groupKey = "导出图片.dl"; break; case "1": //按路口分组 if (string.IsNullOrEmpty(param.SpottingName)) { localPath = "导出图片\\" + fileName; groupKey = "导出图片.dl"; } else { localPath = param.SpottingName + "\\" + fileName; groupKey = param.SpottingName + ".dl"; } break; case "2": //按路口方向分组 if (string.IsNullOrEmpty(param.SpottingName) || string.IsNullOrEmpty(param.DirectionName)) { localPath = "导出图片\\" + fileName; groupKey = "导出图片.dl"; } else { localPath = param.SpottingName + "\\" + param.DirectionName + "\\" + fileName; groupKey = param.SpottingName + "@" + param.DirectionName + ".dl"; } break; case "3": //按路口/日期分组 if (string.IsNullOrEmpty(param.SpottingName) || !param.PassingTime.HasValue) { localPath = "导出图片\\" + fileName; groupKey = "导出图片.dl"; } else { localPath = param.SpottingName + "\\" + param.PassingTime.Value.ToString("yyyy-MM-dd") + "\\" + fileName; groupKey = param.SpottingName + "@" + param.PassingTime.Value.ToString("yyyy-MM-dd") + ".dl"; } break; case "4": //按号牌/日期分组 if (string.IsNullOrEmpty(param.PlateNo) || !param.PassingTime.HasValue) { localPath = "导出图片\\" + fileName; groupKey = "导出图片.dl"; } else { localPath = param.PlateNo + "\\" + param.PassingTime.Value.ToString("yyyy-MM-dd") + "\\" + fileName; groupKey = param.PlateNo + "@" + param.PassingTime.Value.ToString("yyyy-MM-dd") + ".dl"; } break; case "5": //按号牌 if (string.IsNullOrEmpty(param.PlateNo)) { localPath = "导出图片\\" + fileName; groupKey = "导出图片.dl"; } else { //var file= fileName.Split('.'); //string picName=$"{param.PassingTime}-{param.PlateNo}{fileName.Substring(file[0].Length)}" ; localPath = param.PlateNo + "\\" + fileName; groupKey = param.PlateNo + ".dl"; } break; default: localPath = "导出图片\\" + fileName; groupKey = "导出图片.dl"; break; } if (!dictUrls.TryGetValue(groupKey, out realUrls)) { realUrls = new List <string>(); dictUrls[groupKey] = realUrls; } if (picPath) { realUrls.Add(param.Path); } else { realUrls.Add(param.Path + " || " + fileName); } } return(localPath); }