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

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Linq;
 4 None.gif using  System.Text;
 5 None.gif using  System.IO;
 6 None.gif
 7 None.gif namespace  DownloadImagebyXMLListFor2008
 8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 9InBlock.gif    public class HttpDownLoad
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
12InBlock.gif        /// HttpWebRequest Property
13InBlock.gif        /// </summary>
14InBlock.gif        /// <param name="fileName"></param>
15InBlock.gif        /// <param name="url"></param>
16InBlock.gif        /// <param name="localPath"></param>
17ExpandedSubBlockEnd.gif        /// <param name="timeout"></param>

18InBlock.gif        public static void DownloadOneFileByURL(string fileName, string url, string localPath, int timeout)
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            System.Net.HttpWebRequest request = null;
21InBlock.gif            System.Net.HttpWebResponse response = null;
22InBlock.gif            request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url + fileName);
23InBlock.gif            request.Timeout = timeout;//8000 Not work ?
24InBlock.gif            response = (System.Net.HttpWebResponse)request.GetResponse();
25InBlock.gif            Stream s = response.GetResponseStream();
26InBlock.gif            BinaryReader br = new BinaryReader(s);
27InBlock.gif            //int length2 = Int32.TryParse(response.ContentLength.ToString(), out 0);
28InBlock.gif            int length2 = Int32.Parse(response.ContentLength.ToString());
29InBlock.gif            byte[] byteArr = new byte[length2];
30InBlock.gif            s.Read(byteArr, 0, length2);
31ExpandedSubBlockStart.gifContractedSubBlock.gif            if (File.Exists(localPath + fileName)) dot.gif{ File.Delete(localPath + fileName); }
32ExpandedSubBlockStart.gifContractedSubBlock.gif            if (Directory.Exists(localPath) == falsedot.gif{ Directory.CreateDirectory(localPath); }
33InBlock.gif            FileStream fs = File.Create(localPath + fileName);
34InBlock.gif            fs.Write(byteArr, 0, length2);
35InBlock.gif            fs.Close();
36InBlock.gif            br.Close();
37ExpandedSubBlockEnd.gif        }

38ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
39InBlock.gif        ///Web Client Method ,only For Small picture,else large please use FTP
40InBlock.gif        /// </summary>
41InBlock.gif        /// <param name="fileName"></param>
42InBlock.gif        /// <param name="url"></param>
43ExpandedSubBlockEnd.gif        /// <param name="localPath"></param>

44InBlock.gif        public static void DownloadOneFileByURLWithWebClient(string fileName, string url, string localPath)
45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
46InBlock.gif            System.Net.WebClient wc = new System.Net.WebClient();   
47ExpandedSubBlockStart.gifContractedSubBlock.gif            if (File.Exists(localPath + fileName)) dot.gif{ File.Delete(localPath + fileName); }
48ExpandedSubBlockStart.gifContractedSubBlock.gif            if (Directory.Exists(localPath) == falsedot.gif{ Directory.CreateDirectory(localPath); }
49InBlock.gif            wc.DownloadFile(url + fileName, localPath + fileName);
50ExpandedSubBlockEnd.gif        }

51ExpandedSubBlockEnd.gif    }

52ExpandedBlockEnd.gif}

53 None.gif
需要注意点:

第一 DownloadOneFileByURL方法,有时会下载不了文件,如果文件大于40K就更明显DownloadOneFileByURLWithWebClient则无此问题。当然,这个大文件也是相对的,如果真的large或huge,请参考FTP。

 

http://www.cnblogs.com/downmoon/archive/2008/01/29/1057726.html 

第二 调用时请用Thread ,给出一个示例
 1 None.gif   private   void  btnGet_Click( object  sender, EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            if (txtTempFile.Text.Trim().Length == 0)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 5InBlock.gif                ErrorStop("列表文件为空!"); return;
 6ExpandedSubBlockEnd.gif            }

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

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

23ExpandedSubBlockStart.gifContractedSubBlock.gif                    catch dot.gifcontinue; }
24ExpandedSubBlockEnd.gif                }

25ExpandedSubBlockEnd.gif            }

26ExpandedBlockEnd.gif        }

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


转载于:https://www.cnblogs.com/downmoon/archive/2008/06/11/1217269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值