一个批量下载图片的c#类(downmoon)
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
/// 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,else large please use FTP
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
}
53
需要注意点:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

第一 DownloadOneFileByURL方法,有时会下载不了文件,如果文件大于40K就更明显,DownloadOneFileByURLWithWebClient则无此问题。当然,这个大文件也是相对的,如果真的large或huge,请参考FTP。
http://www.cnblogs.com/downmoon/archive/2008/01/29/1057726.html
第二 调用时请用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<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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

附 WebClient类的说明
http://msdn.microsoft.com/zh-cn/library/system.net.webclient(VS.80).aspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)