下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片

将一台服务器的文件、图片,保存(下载)到另外一台服务器进行保存的方法:

  1         #region 图片下载
  2 
  3         #region 图片下载【使用流、WebRequest进行保存】
  4         /// <summary>
  5         /// 图片下载【使用流、WebRequest进行保存】
  6         /// </summary>
  7         /// <param name="fileUrl">图片URL地址(例如:http://img.baidu.com/video/img/video_logo_new.gif) </param>
  8         /// <param name="path">存储到本地(服务器)路径(例如:Upload/Image)</param>
  9         /// <param name="fileName">文件名(包含后缀名)</param>
 10         /// <param name="fileFormat">图片格式</param>
 11         public static void WebRequestDownloadFileImage(string fileUrl, string path, string fileName, System.Drawing.Imaging.ImageFormat fileFormat)
 12         {
 13             try
 14             {
 15                 path = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", path));
 16                 if (!Directory.Exists(path))  //判断目录是否存在
 17                 {
 18                     Directory.CreateDirectory(path);//创建该文件
 19                 }
 20                 WebRequest wreq = WebRequest.Create(fileUrl);
 21                 using (HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse())
 22                 {
 23                     Stream s = wresp.GetResponseStream();
 24                     System.Drawing.Image img;
 25                     img = System.Drawing.Image.FromStream(s);
 26                     path = path + fileName;
 27                     img.Save(path, fileFormat);   //保存 
 28                 }
 29             }
 30             catch (Exception ex)
 31             {
 32                 throw;
 33             }
 34         }
 35         #endregion
 36 
 37         #region 图片下载【使用流、WebClient进行保存】
 38         /// <summary>
 39         /// 图片下载【使用流、WebClient进行保存】
 40         /// </summary>
 41         /// <param name="fileUrl">图片URL地址(例如:http://img.baidu.com/video/img/video_logo_new.gif) </param>
 42         /// <param name="path">存储到本地(服务器)路径(例如:Upload/Image)</param>
 43         /// <param name="fileName">文件名称(包含后缀名)</param>
 44         /// <param name="fileFormat">图片格式</param>
 45         public static void WebClientDownloadFileImage(string fileUrl, string path, string fileName, System.Drawing.Imaging.ImageFormat fileFormat)
 46         {
 47             try
 48             {
 49                 path = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", path));
 50                 if (!Directory.Exists(path))  //判断目录是否存在
 51                 {
 52                     Directory.CreateDirectory(path);//创建该文件
 53                 }
 54                 WebClient webClient = new WebClient();
 55                 byte[] imgByte;
 56                 imgByte = webClient.DownloadData(fileUrl);
 57                 using (MemoryStream ms = new MemoryStream(imgByte))
 58                 {
 59                     System.Drawing.Image img;
 60                     img = System.Drawing.Image.FromStream(ms);
 61                     path = path + fileName;
 62                     img.Save(path, fileFormat);   //保存 
 63                 }
 64             }
 65             catch (Exception ex)
 66             {
 67                 throw;
 68             }
 69 
 70         }
 71         #endregion
 72         #endregion
 73 
 74         #region 文件下载(从第三方服务器下载到本服务器)
 75 
 76         #region 下载(第三方)远程文件保存到本地(自己服务器)的方法、保存抓取远程图片【异步下载】
 77         /// <summary>
 78         /// 下载(第三方)远程图片保存到本地(自己服务器)的方法、保存抓取远程图片 【异步下载】
 79         /// </summary>
 80         /// <param name="fileUrl">文件URL地址(例如:http://img.baidu.com/video/img/video_logo_new.gif) </param>
 81         /// <param name="path">存储到本地(服务器)路径(例如:Upload/Image)</param>
 82         /// <param name="fileName">文件名称(包括后缀名)(例如:login.jpg)</param>
 83         public static void DownloadFileAsync(string fileUrl, string path, string fileName)
 84         {
 85             try
 86             {
 87                 System.Net.WebClient client = new System.Net.WebClient();
 88                 path = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", path));
 89                 if (!Directory.Exists(path))  //判断目录是否存在
 90                 {
 91                     Directory.CreateDirectory(path);//创建该文件
 92                 }
 93                 path = path + fileName;
 94                 client.DownloadFileAsync(new Uri(fileUrl, UriKind.RelativeOrAbsolute), path);
 95             }
 96             catch (Exception ex)
 97             {
 98 
 99             }
100         }
101         #endregion
102 
103         #region 下载(第三方)远程文件保存到本地(自己服务器)的方法、保存抓取远程图片【同步下载】
104         /// <summary>
105         /// 下载(第三方)远程图片保存到本地(自己服务器)的方法、保存抓取远程图片 【同步下载】
106         /// </summary>
107         /// <param name="fileUrl">文件URL地址(例如:http://img.baidu.com/video/img/video_logo_new.gif) </param>
108         /// <param name="path">存储到本地(服务器)路径(例如:Upload/Image)</param>
109         /// <param name="fileName">文件名称(包括后缀名)(例如:login.jpg)</param>
110         public static void DownloadFile(string fileUrl, string path, string fileName)
111         {
112             try
113             {
114                 System.Net.WebClient client = new System.Net.WebClient();
115                 path = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", path));
116                 if (!Directory.Exists(path))  //判断目录是否存在
117                 {
118                     Directory.CreateDirectory(path);//创建该文件
119                 }
120                 path = path + fileName;
121                 client.DownloadFile(new Uri(fileUrl, UriKind.RelativeOrAbsolute), path);
122             }
123             catch (Exception ex)
124             {
125 
126             }
127         }
128         #endregion
129 
130         #endregion
原文地址:https://www.cnblogs.com/linJie1930906722/p/5524664.html