static void Main(string[] args) { KVStoreManager.SetEngine(new KVStoreEngine()); AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); while (true) { List <ViewModel.DownloadEntity> downloadTasks = null; //如果出现异常,重试4次 var queryFlag = 0; for (int i = 0; i < 5; i++) { try { if (queryFlag <= 0) { var downQo = new DownloadQO(); downQo.Equal(DownloadQO.QueryEnums.Disabled, 0).OrderByAsc("Disabled"); downloadTasks = mediaService.GetDownloadDtosByPara(downQo, false); if (downloadTasks == null || !downloadTasks.Any()) { Console.WriteLine("当前批次下载任务已完成,等待10秒继续扫描待下载的任务...".Log()); System.Threading.Thread.Sleep(10000); continue; } queryFlag += 1; } else { break; } if (i == 4) { Console.WriteLine("当前程序已重试4次,未正常获取到数据,程序正在退出...".Log()); Environment.Exit(-1);//退出 } } catch (Exception e) { Console.WriteLine($"查询下载任务出现异常,异常信息:{e.Message},正在进行第{i + 1}次重试...".Log()); queryFlag -= 1; } } //生成下载任务,并保存到指定目录 for (int i = 0; i < downloadTasks.Count; i++) { try { Console.WriteLine($"[正在下载资源:{downloadTasks[i].HttpUrl}][{DateTime.Now}]".Log()); // 设置参数 var httpUrl = downloadTasks[i].HttpUrl; Stream rps = null; try { HttpWebRequest rq = WebRequest.Create(httpUrl) as HttpWebRequest; //发送请求并获取相应回应数据 HttpWebResponse rp = rq.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 rps = rp.GetResponseStream(); } catch (Exception e) { Console.WriteLine($"下载出现异常:{e.Message}".Log()); continue; } //目标目录 var insDir = Environment.CurrentDirectory + "\\" + downloadTasks[i].DirName; //创建本地文件写入流 if (!Directory.Exists(insDir)) { Directory.CreateDirectory(insDir); } //网络资源文件是否已下载 var fileReg = new Regex("(\\d+_){3}\\w*.(jpg|jpeg|png|mp4|flv|gif)"); var sourceFileName = fileReg.Match(httpUrl).Value; //校验目标目录中的文件是否已存在,如果存在则跳过,否则下载 if (File.Exists(insDir + "\\" + sourceFileName)) { Console.WriteLine($"[{sourceFileName}][此资源已下载,跳过]!".Log()); continue; } Stream st = new FileStream(insDir + $"\\{sourceFileName}", FileMode.Create); byte[] bar = new byte[1024]; int sz = rps.Read(bar, 0, (int)bar.Length); while (sz > 0) { st.Write(bar, 0, sz); sz = rps.Read(bar, 0, (int)bar.Length); } st.Close(); rps.Close(); Console.WriteLine("[资源下载完成!]".Log()); downloadTasks[i].Disabled = 1; downloadTasks[i].Rec_ModifyBy = sys; downloadTasks[i].Rec_ModifyTime = DateTime.Now; mediaService.UpdateDownload(downloadTasks[i]); } catch (Exception e) { throw e; } } Console.WriteLine($"当前批次下载任务已完成,等待10秒继续扫描待下载的任务...[{DateTime.Now}]".Log()); System.Threading.Thread.Sleep(10000); } }
//下载 private async void button2_Click(object sender, EventArgs e) { await System.Threading.Tasks.Task.Run(() => { while (!DownloadCancelToken.IsCancellationRequested) { #region 获取数据库已上传的下载资源地址 List <DownloadEntity> downloadTasks = null; //如果出现异常,重试4次 var queryFlag = 0; for (int i = 0; i < 5; i++) { try { if (queryFlag <= 0) { var downQo = new DownloadQO(); downQo.Equal(DownloadQO.QueryEnums.Disabled, 0).OrderByAsc("Disabled"); downloadTasks = mediaService.GetDownloadDtosByPara(downQo, false); if (downloadTasks == null || !downloadTasks.Any()) { Print("当前批次下载任务已完成,等待10秒继续扫描待下载的任务...".Log(), false); Thread.Sleep(10000); continue; } queryFlag += 1; } else { break; } if (i == 4) { Print("当前程序已重试4次,未正常获取到数据,程序正在退出...".Log(), false); Environment.Exit(-1);//退出 } } catch (Exception ex) { Print($"查询下载任务出现异常,异常信息:{ex.Message},正在进行第{i + 1}次重试...".Log(), false); queryFlag -= 1; } } #endregion #region 执行下载任务,并保存到指定目录 for (int i = 0; i < downloadTasks.Count; i++) { if (!DownloadCancelToken.IsCancellationRequested) { try { Print($"[正在下载资源:{downloadTasks[i].HttpUrl}][{DateTime.Now}]".Log(), false); // 设置参数 var httpUrl = downloadTasks[i].HttpUrl; Stream rps = null; try { HttpWebRequest rq = WebRequest.Create(httpUrl) as HttpWebRequest; HttpWebResponse rp = rq.GetResponse() as HttpWebResponse; rps = rp.GetResponseStream(); } catch (Exception ex) { Print($"下载出现异常:{ex.Message}".Log(), false); continue; } //目标目录 var insDir = Environment.CurrentDirectory + "\\" + downloadTasks[i].DirName; //创建本地文件写入流 if (!Directory.Exists(insDir)) { Directory.CreateDirectory(insDir); } //网络资源文件是否已下载 var fileReg = new Regex("(\\d+_){3}\\w*.(jpg|jpeg|png|mp4|flv|gif)"); var sourceFileName = fileReg.Match(httpUrl).Value; //校验目标目录中的文件是否已存在,如果存在则跳过,否则下载 if (File.Exists(insDir + "\\" + sourceFileName)) { Print($"[{sourceFileName}][此资源已下载,跳过]!".Log(), false); continue; } Stream st = new FileStream(insDir + $"\\{sourceFileName}", FileMode.Create); byte[] bar = new byte[1024]; int sz = rps.Read(bar, 0, (int)bar.Length); while (sz > 0) { st.Write(bar, 0, sz); sz = rps.Read(bar, 0, (int)bar.Length); } st.Close(); rps.Close(); Print($"[{sourceFileName}][资源下载完成!]".Log(), false); downloadTasks[i].Disabled = 1; downloadTasks[i].Rec_ModifyBy = sys; downloadTasks[i].Rec_ModifyTime = DateTime.Now; mediaService.UpdateDownload(downloadTasks[i]); } catch (Exception ex) { ex.Message.Log(); } } else { break; } } Print($"当前批次下载任务已完成,等待10秒继续扫描待下载的任务...[{DateTime.Now}]".Log(), false); Thread.Sleep(10000); #endregion } }, DownloadCancelToken.Token); }