技术开发 频道

一个批量下载图片的c#类(downmoon)


【IT168技术文档】

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.IO; 6 7namespace DownloadImagebyXMLListFor2008 8{ 9 public class HttpDownLoad 10 { 11 /**//// <summary> 12 /// HttpWebRequest Property 13 /// </summary> 14 /// <param name="fileName"></param> 15 /// <param name="url"></param> 16 /// <param name="localPath"></param> 17 /// <param name="timeout"></param> 18 public static void DownloadOneFileByURL(string fileName, string url, string localPath, int timeout) 19 { 20 System.Net.HttpWebRequest request = null; 21 System.Net.HttpWebResponse response = null; 22 request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url + fileName); 23 request.Timeout = timeout;//8000 Not work ? 24 response = (System.Net.HttpWebResponse)request.GetResponse(); 25 Stream s = response.GetResponseStream(); 26 BinaryReader br = new BinaryReader(s); 27 //int length2 = Int32.TryParse(response.ContentLength.ToString(), out 0); 28 int length2 = Int32.Parse(response.ContentLength.ToString()); 29 byte[] byteArr = new byte[length2]; 30 s.Read(byteArr, 0, length2); 31 if (File.Exists(localPath + fileName)) { File.Delete(localPath + fileName); } 32 if (Directory.Exists(localPath) == false) { Directory.CreateDirectory(localPath); } 33 FileStream fs = File.Create(localPath + fileName); 34 fs.Write(byteArr, 0, length2); 35 fs.Close(); 36 br.Close(); 37 } 38 /**//// <summary> 39 ///Web Client Method ,only For Small picture 40 /// </summary> 41 /// <param name="fileName"></param> 42 /// <param name="url"></param> 43 /// <param name="localPath"></param> 44 public static void DownloadOneFileByURLWithWebClient(string fileName, string url, string localPath) 45 { 46 System.Net.WebClient wc = new System.Net.WebClient(); 47 if (File.Exists(localPath + fileName)) { File.Delete(localPath + fileName); } 48 if (Directory.Exists(localPath) == false) { Directory.CreateDirectory(localPath); } 49 wc.DownloadFile(url + fileName, localPath + fileName); 50 } 51 } 52}
0
相关文章