从URL获取图片并保存到本地

     ///   <summary> 
        ///  HttpWebRequest Property
        ///   </summary> 
        ///   <param name="fileName">文件名称</param> 
        ///   <param name="url">图片地址</param> 
        ///   <param name="localPath">本地路径("D:\\imgdow")</param>       
        public static void DownloadOneFileByURL(string fileName, string url, string localPath)
        {
            Stream s = null;
            System.Net.HttpWebRequest request = null;
            System.Net.HttpWebResponse response = null;
            request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.Timeout = 5000; // 超时时间
            response = (System.Net.HttpWebResponse)request.GetResponse();
            s = response.GetResponseStream();
            var imgs = Image.FromStream(s);
            imgs.Save(localPath + $"/{fileName}.jpg");
            s.Close();
        }

 

posted @ 2018-01-15 11:51  爱坐枫林晚  阅读(2395)  评论(0编辑  收藏  举报