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

<!-- {cps..0}-->1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6
7 namespace DownloadImagebyXMLListFor2008
8 {
9 public class HttpDownLoad
10 {
11 /**/ /// <summary>
12 /// HttpWebRequestProperty
13 /// </summary>
14 /// <paramname="fileName"></param>
15 /// <paramname="url"></param>
16 /// <paramname="localPath"></param>
17 /// <paramname="timeout"></param>

18 public static void DownloadOneFileByURL( string fileName, string url, string localPath, int timeout)
19 {
20 System.Net.HttpWebRequestrequest = null ;
21 System.Net.HttpWebResponseresponse = null ;
22 request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url + fileName);
23 request.Timeout = timeout; // 8000Notwork?
24 response = (System.Net.HttpWebResponse)request.GetResponse();
25 Streams = response.GetResponseStream();
26 BinaryReaderbr = new BinaryReader(s);
27 // intlength2=Int32.TryParse(response.ContentLength.ToString(),out0);
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 FileStreamfs = File.Create(localPath + fileName);
34 fs.Write(byteArr, 0 ,length2);
35 fs.Close();
36 br.Close();
37 }

38 /**/ /// <summary>
39 /// WebClientMethod,onlyForSmallpicture,
else large please use FTP
40 /// </summary>
41 /// <paramname="fileName"></param>
42 /// <paramname="url"></param>
43 /// <paramname="localPath"></param>

44 public static void DownloadOneFileByURLWithWebClient( string fileName, string url, string localPath)
45 {
46 System.Net.WebClientwc = 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 }

53

需要注意点:
第一 DownloadOneFileByURL方法,有时会下载不了文件,如果文件大于40K就更明显 DownloadOneFileByURLWithWebClient则无此问题。 当然,这个大文件也是相对的,如果真的large或huge,请参考FTP。
http://blog.csdn.net/downmoon/archive/2008/01/29/2071776.aspx
第二 调用时请用Thread
, 给出一个示例

<!-- {cps..1}-->1 private void btnGet_Click( object sender,EventArgse)
2 {
3 if (txtTempFile.Text.Trim().Length == 0 )
4 {
5 ErrorStop( " 列表文件为空! " ); return ;
6 }

7 System.Threading.Threadthread = new System.Threading.Thread( new System.Threading.ThreadStart(DownloadAll));
8 thread.Start();
9 }

10 private void DownloadAll()
11 {
12 List < string > ls = GetStringsByFile(txtTempFile.Text.Trim());
13 if ( null != ls)
14 {
15
16 foreach ( string s in ls)
17 {
18 try
19 {
20 // HttpDownLoad.DownloadOneFileByURL(s,Globals.HttpPreUrl,Globals.LocalPrePath,8000000);
21 HttpDownLoad.DownloadOneFileByURLWithWebClient(s,Globals.HttpPreUrl,Globals.LocalPrePath);
22 }

23 catch { continue ;}
24 }

25 }

26 }


附 WebClient类的说明  
http://msdn.microsoft.com/zh-cn/library/system.net.webclient(VS.80).aspx

阅读更多

没有更多推荐了,返回首页