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

Posted 爱坐枫林晚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从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();
        }

 

以上是关于从URL获取图片并保存到本地的主要内容,如果未能解决你的问题,请参考以下文章

java通过百度地图 获取静态图片 如何保存到本地

Python爬虫获取图片并下载保存至本地的实例

Python抓取网页中的图片到本地

python爬取某个网站的图片并保存到本地

使用URLConnection下载文件或图片并保存到本地

获取网络图片并保存到指定目录