c#一個批次下載圖片的類

dunne21發表於2021-09-09

 直接上程式碼:

 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        /// 
12        /// HttpWebRequest Property
13        /// 
14        /// 
15        /// 
16        /// 
17        /// 
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        /// 
39        ///Web Client Method ,only For Small picture,else large please use FTP
40        /// 
41        /// 
42        /// 
43        /// 
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}

需要注意點:

第一 DownloadOneFileByURL方法,有時會下載不了檔案,如果檔案大於40K就更明顯,DownloadOneFileByURLWithWebClient則無此問題。當然,這個大檔案也是相對的。

 

第二 呼叫時請用Thread,給出一個示例:

 1 private void btnGet_Click(object sender, EventArgs e)
 2        {
 3            if (txtTempFile.Text.Trim().Length == 0)
 4            {
 5                ErrorStop("列表檔案為空!"); return;
 6            }
 7            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DownloadAll));
 8            thread.Start();
 9        }
10 private void DownloadAll()
11        {
12            List 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        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1600/viewspace-2800492/,如需轉載,請註明出處,否則將追究法律責任。

相關文章