public void DownLoadSoundingImg() { DateTime dt = DateTime.Now.Hour - 12 > 0 ? DateTime.Now : DateTime.Now.AddDays(-1);; string year = dt.Year.ToString(); string month = dt.Month.ToString(); string date = dt.Day.ToString(); string state = dt.Hour - 12 > 0 ? "00" : "12"; string targetUrl = "http://weather.uwyo.edu/cgi-bin/sounding?region=seasia&TYPE=GIF%3ASKEWT&YEAR=" + year + "&MONTH=" + month + "&FROM=" + date + state + "&TO=" + date + state + "&STNM=53772"; string html = GetPage(targetUrl); string strdate = dt.ToString("yyyyMMdd") + state; //网页路径 string webpath = "http://weather.uwyo.edu/upperair/bimages/" + strdate + ".53772.skewt.parc.gif"; string fileName = dt.ToString("yyyyMMddHHmm") + ".GIF"; string locationUrl = ConfigurationManager.AppSettings["fullpath"] + "SoundingImg\\" + fileName; //保存到本地路径 DateTime CurrTime = Convert.ToDateTime(dt.ToString("yyyy-MM-dd") + " " + state + ":00"); //文件时间 string relativePath = "SoundingImg\\" + fileName; //文件相对路径 T_Moni_ImgFiles img = new T_Moni_ImgFiles(); img.FileName = fileName; img.FullFileName = relativePath; img.Cate = 7; img.CreatedTime = DateTime.Now; img.FileDate = CurrTime; ImgAcp(webpath, locationUrl, img); }
/// <summary> /// 图片采集 /// </summary> private void ImgAcp(string imgUrl, string localpath, T_Moni_ImgFiles imgfile) { if (GetUrlError(imgUrl) != 200) { writelog("图片未找到:" + imgUrl); return; } WebRequest request = WebRequest.Create(imgUrl); WebResponse response = null; try { response = request.GetResponse(); request.Timeout = 5 * 60 * 1000; ImageFormat f = ImageFormat.Gif; string fileext = imgUrl.Substring(imgUrl.LastIndexOf('.')); if (fileext.ToUpper() == ".JPG") { f = ImageFormat.Jpeg; } else if (fileext.ToUpper() == ".PNG") { f = f = ImageFormat.Png; } else if (fileext.ToUpper() == ".BMP") { f = ImageFormat.Bmp; } Stream resStream = response.GetResponseStream(); FileInfo fi = new FileInfo(localpath); //如果存在文件则先干掉 if (!fi.Exists) { System.Drawing.Image img; img = System.Drawing.Image.FromStream(resStream); img.Save(localpath, f); resStream.Close(); string ConnectionString = ConfigurationManager.ConnectionStrings["DB"].ConnectionString; SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); string sql = "insert into T_Mid_ImgFiles(FileName,Cate,FileDate,FullFileName,CreatedTime) values('" + imgfile.FileName + "'," + imgfile.Cate + ",'" + imgfile.FileDate + "','" + imgfile.FullFileName + "','" + imgfile.CreatedTime + "')"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); //EntityHelper.Add<T_Moni_ImgFiles>(imgfile); } writelog("图片同步成功:" + localpath); } catch (Exception ex) { writelog("图片同步出错:" + ex.Message.ToString() + ">>" + imgUrl); } finally { response.Close(); response.Dispose(); } }