/// <summary> /// 检测是否有需要下载的广告文件 /// </summary> public void AdvertUploadMon() { DbOper dbOper = new DbOper(); dbOper.DbFileName = _m_DbFileName; try { dbOper.ConnType = ConnectType.KeepConn; string strSql = @"select AdvListID,FileCode,DownUrl,FileName,FileFormat from T_ADV_ADVFILE where ImportType = '1' and Status in ('0','1','3')"; DataSet dataSet = dbOper.dataSet(strSql); if (dataSet.Tables.Count > 0) { int intCount = dataSet.Tables[0].Rows.Count; bool result = false; if (intCount > 0) { AdvertInfoModel advertInfo = new AdvertInfoModel(); for (int i = 0; i < intCount; i++) { advertInfo.AdvListID = dataSet.Tables[0].Rows[i]["AdvListID"].ToString(); advertInfo.FileCode = dataSet.Tables[0].Rows[i]["FileCode"].ToString(); advertInfo.FileName = dataSet.Tables[0].Rows[i]["FileName"].ToString(); advertInfo.DownUrl = dataSet.Tables[0].Rows[i]["DownUrl"].ToString(); advertInfo.FileType = dataSet.Tables[0].Rows[i]["FileFormat"].ToString(); result = DownAdvertFile(advertInfo); if (result) { // 下载成功,保存广告文件状态 strSql = "update T_ADV_ADVFILE set Status = '2' where AdvListID = '" + advertInfo.AdvListID + "' and FileCode = '" + advertInfo.FileCode + "'"; result = dbOper.excuteSql(strSql); } } } } } catch { } finally { dbOper.closeConnection(); } }
/// <summary> /// 更新广告文件状态 /// </summary> /// <param name="advertInfo"></param> /// <param name="status">0:等待更新 1:更新中 2:成功 3:失败</param> /// <returns></returns> public bool UpdateAdvFileStatus(AdvertInfoModel advertInfo, string status) { bool result = false; DbOper dbOper = new DbOper(); dbOper.DbFileName = _m_DbFileName; try { string strSql = "update T_ADV_ADVFILE set Status = '" + status + "' where AdvListID = '" + advertInfo.AdvListID + "' and FileCode = '" + advertInfo.FileCode + "'"; result = dbOper.excuteSql(strSql); } catch { result = false; } finally { dbOper.closeConnection(); } return(result); }
/// <summary> /// 远程下载广告文件 /// </summary> /// <param name="advertInfo"></param> /// <returns></returns> private bool DownAdvertFile(AdvertInfoModel advertInfo) { string strFileLocalPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "advert\\" + advertInfo.AdvListID + "\\" + advertInfo.FileName + "." + advertInfo.FileType; // 检测目录下是否有需要下载的正式文件 if (System.IO.File.Exists(strFileLocalPath)) { // 有正式的文件,无需下载 return(true); } // 打开上次下载的文件或新建文件 long lStartPos = 0; System.IO.FileStream fs; // 获取本地文件的存储位置目录以及文件名称(带文件格式) string strFileName = advertInfo.FileName; string strFilePath = advertInfo.FileName + "." + advertInfo.FileType; if ((string.IsNullOrEmpty(strFileName)) || (string.IsNullOrEmpty(strFilePath))) { return(false); } // 生成下载临时文件 string strFileTemp = strFileLocalPath + ".tmp"; if (System.IO.File.Exists(strFileTemp)) { fs = System.IO.File.OpenWrite(strFileTemp); lStartPos = fs.Length; fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针 } else { fs = new System.IO.FileStream(strFileTemp, System.IO.FileMode.Create); lStartPos = 0; } //打开网络连接 bool uploadResult = false; try { System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(advertInfo.DownUrl); if (lStartPos > 0) { request.AddRange((int)lStartPos); //设置Range值 } //向服务器请求,获得服务器回应数据流 System.IO.Stream ns = request.GetResponse().GetResponseStream(); byte[] nbytes = new byte[1024]; int nReadSize = 0; nReadSize = ns.Read(nbytes, 0, 1024); while (nReadSize > 0) { fs.Write(nbytes, 0, nReadSize); nReadSize = ns.Read(nbytes, 0, 1024); } ns.Close(); uploadResult = true; return(true); } catch { return(false); } finally { fs.Close(); // 下载成功,把临时文件修改成正式文件 if (uploadResult) { Thread.Sleep(200); try { if (System.IO.File.Exists(strFileTemp)) { System.IO.File.Copy(strFileTemp, strFileLocalPath, true); Thread.Sleep(200); System.IO.File.Delete(strFileTemp); } } catch { } } } }