using MySql.Data.MySqlClient; using parkMonitor.LOG; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace centralController.advert { public class AdvertManager { /// /// 从数据库获取URL,更新本地广告文件 /// /// public bool UpdateAdvert(out string notification) { bool result = true; notification = ""; List urls = new List(); if (Monitor.Monitor.remoteDBOper != null) { //获取URLs string getAdvertUrlSql = "select advertUrl from advert;"; MySqlDataReader reader = Monitor.Monitor.remoteDBOper.Query(getAdvertUrlSql); if (reader != null) { try { while (reader.Read() && reader.HasRows) { urls.Add(reader.GetString("advertUrl")); } } catch { result = false; } try { reader.Close(); reader.Dispose(); } catch { result = false; } } notification += "url读取结果:\n"; //删除本地文件,下载url中文件到本地 try { if (!Directory.Exists(Monitor.Monitor.advertPath)) { Directory.CreateDirectory(Monitor.Monitor.advertPath); } } catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "路径创建异常"); result = false; } try { string[] fileList = Directory.GetFileSystemEntries(Monitor.Monitor.advertPath); if (fileList.Length == 0) { for (int i = 0; i < fileList.Length; i++) { File.Delete(fileList[i]); } } } catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "未找到需删除文件或删除文件异常\n" + e.Message); } try { if (Directory.Exists(Monitor.Monitor.advertPath + @"\temp")) Directory.Delete(Monitor.Monitor.advertPath + @"\temp", true); } catch (Exception e) { Log.WriteLog(LogType.process, LogFile.ERROR, "未找到需删除文件夹或删除文件夹异常\n" + e.Message); } if (result && urls.Count() > 0) { List.Enumerator enumer = urls.GetEnumerator(); while (enumer.MoveNext()) { string fileName = Path.GetFileName(enumer.Current); bool response = HttpDownload(enumer.Current, Monitor.Monitor.advertPath + @"\" + fileName); result = result || response; notification += (response?"成功:":"失败:")+fileName+"\n"; } } } return result; } /// /// http下载文件 /// /// 下载文件地址 /// 文件存放地址,包含文件名 /// private bool HttpDownload(string url, string path) { string tempPath = System.IO.Path.GetDirectoryName(path) + @"\temp"; System.IO.Directory.CreateDirectory(tempPath); //创建临时文件目录 string tempFile = tempPath + @"\" + System.IO.Path.GetFileName(path) + ".temp"; //临时文件 if (System.IO.File.Exists(tempFile)) { System.IO.File.Delete(tempFile); //存在则删除 } try { FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); //创建本地文件写入流 //Stream stream = new FileStream(tempFile, FileMode.Create); byte[] bArr = new byte[1024]; int size = responseStream.Read(bArr, 0, (int)bArr.Length); while (size > 0) { //stream.Write(bArr, 0, size); fs.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, (int)bArr.Length); } //stream.Close(); fs.Close(); responseStream.Close(); System.IO.File.Move(tempFile, path); return true; } catch (Exception ex) { Log.WriteLog(LogType.process, LogFile.ERROR, "获取url:" + url + "失败\n" + ex.Message); return false; } } } }