因为项目上需要加载在线卫星云图,因此写了这个功能来把卫星云图下载的本地,在这里记录一下:
string imageUrl=“http://image.nmc.cn/product/2018/08/06/WXCL/SEVP_NSMC_WXCL_ASC_E99_ACHN_LNO_PY_20180806051500000.JPG?v=1533532726321”;
string _ImagePath = AppDomain.CurrentDomain.BaseDirectory + "Resources\\CloudImages\\"+“test.JPG”;
上面为图片地址和本地文件夹的设置。
接下来是图片下载相关代码:
HttpWebRequest request = HttpWebRequest.Create(imageUrl) as HttpWebRequest; HttpWebResponse response = null; response = request.GetResponse() as HttpWebResponse;if (response.StatusCode != HttpStatusCode.OK) continue; Stream reader = response.GetResponseStream(); FileStream writer = new FileStream(_ImagePath, FileMode.OpenOrCreate, FileAccess.Write); byte[] buff = new byte[512]; int c = 0; //实际读取的字节数 while ((c = reader.Read(buff, 0, buff.Length)) > 0) { writer.Write(buff, 0, c); } writer.Close(); writer.Dispose(); reader.Close(); reader.Dispose(); response.Close();