protected void Page_Load(object sender, EventArgs e) { //验证 Base_Users userExt = AdminCookie.GetUserFromCookie( ); if (userExt == null || userExt.UserID <= 0) { return; } //路径处理 if (TextUtility.EmptyTrimOrNull(clientPath) || clientPath == "/" || !clientPath.StartsWith("/upload")) { clientPath = "/upload"; } //操作处理 HttpFileManager fileManage = new HttpFileManager(Request.QueryString["act"]); builder.Append(fileManage.Value); if (builder.Length > 0) { SetUploadFilePath(builder.ToString( ).Replace("'", "\\'").Replace("\r\n", ""), fileManage.UploadFilePath); } }
private async void DownloadPictures(CancellationToken token) { await Task.Factory.StartNew(() => { GetUrls(); if (UrlList != null) { Parallel.For(0, UrlList.Count, (i) => { while (CurrentPage < 500) { var httpUrl = $"{UrlList[i]}&page={CurrentPage}"; Print($"开始加载第【{CurrentPage}】页资源:{httpUrl}"); var listHtml = string.Empty; try { PageList: listHtml = HttpFileManager.GetHttpUrlString(httpUrl); if (string.IsNullOrEmpty(listHtml)) { Print("网络出现异常,30秒后重试".Log()); Thread.Sleep(1000 * 30);//等待30秒再试下一个 goto PageList; } } catch (Exception ex) { Print($"加载{httpUrl}出现异常,跳至下一个帖子{ex.Message}".Log()); continue; } Print($"资源加载成功,使用正则匹配可下载的资源"); var reg = new Regex("\\<a href\\=\"(?<href>htm_data[/0-9-a-z]+\\.html)\"\\sid[^>]+\\>(?<text>[^>]+)\\</a\\>"); var listMatchs = reg.Matches(listHtml); if (listMatchs != null && listMatchs.Count > 0) { var _basePath = string.Empty; Print($"匹配成功,当前匹配的是第{CurrentPage}页内容"); if (UrlList[i].Contains("fid=15")) { _basePath = AppSettings.GetValue <string>("SaveDir") + "\\zp"; } else if (UrlList[i].Contains("fid=14")) { _basePath = AppSettings.GetValue <string>("SaveDir") + "\\xz"; } else if (UrlList[i].Contains("fid=16")) { _basePath = AppSettings.GetValue <string>("SaveDir") + "\\lc"; } for (int j = 0; j < listMatchs.Count; j++) { var dir = _basePath + "\\" + listMatchs[j].Groups["text"].Value; var htmlDetailUrl = Domain + "/" + listMatchs[j].Groups["href"].Value; Print($"检查目录{dir}是否存在"); if (!Directory.Exists(dir)) { Print($"目录不存在,正在创建目录"); Directory.CreateDirectory(dir); } Print($"目录已创建,正在浏览当前页第{j + 1}个帖子"); var detailHtml = HttpFileManager.GetHttpUrlString(htmlDetailUrl); if (!string.IsNullOrEmpty(detailHtml)) { var detailReg = new Regex("(?<img>http:[^'\"]+\\.(jpg|jpeg|png|gif))"); Print($"正在匹配链接加载成功后的可下载资源"); var matchs = detailReg.Matches(detailHtml); //去重 if (matchs != null && matchs.Count > 0) { var matchsResult = matchs.Cast <Match>().GroupBy(g => g.Groups["img"].Value).Distinct().ToList(); Print($"匹配成功,共{matchsResult.Count()}条资源可下载"); if (Directory.Exists(dir) && Directory.GetFiles(dir).Count() >= matchsResult.Count()) { Print("此帖图片已全部下载,下一个"); continue; } for (int x = 0; x < matchsResult.Count; x++) { var fileUrl = matchsResult[x].Key; var fileName = fileUrl.Substring(fileUrl.LastIndexOf('/') + 1); var filePath = dir + "\\" + fileName; Print($"[{x}]正在将文件{fileName}存放至{dir}"); if (File.Exists(filePath))//文件存在,Pass { //文件上次未下载成功,重新下载 if (new FileInfo(filePath).Length >= 35 * 1024) { Print($"文件[{fileName}]已存在,下一个..."); continue; } } try { Task.Factory.StartNew(() => { HttpFileManager.DownloadFile(fileUrl, filePath); }); } catch (Exception e) { Print("下载失败," + e.Message + ",继续下一个图片的下载".Log()); continue; } Print($"文件下载成功"); //Task.Run(() => //{ // ShowPicture(dir + "\\" + fileName); //}); } } } Print($"当前帖子图片已全部下载完毕"); } } CurrentPage += 1; } }); } else { MessageBox.Show("请选择一个列表页"); return; } }, token); }