一个批量下载图片的c#类(downmoon)
来源:互联网 发布:网络口碑营销失败案例 编辑:程序博客网 时间:2024/05/21 07:46
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,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
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,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
需要注意点:
第一 DownloadOneFileByURL方法,有时会下载不了文件,如果文件大于40K就更明显,DownloadOneFileByURLWithWebClient则无此问题。当然,这个大文件也是相对的,如果真的large或huge,请参考FTP。
http://blog.csdn.net/downmoon/archive/2008/01/29/2071776.aspx
第二 调用时请用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 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 }
附 WebClient类的说明
http://msdn.microsoft.com/zh-cn/library/system.net.webclient(VS.80).aspx
- 一个批量下载图片的c#类(downmoon)
- 一个批量下载其它机子图片的c#类(downmoon)
- 一个批量处理数组的方法(downmoon原创)
- Unity3D C#批量下载图片
- 一个自动动态播放图片的类(downmoon)新增图片效果
- 一个自动动态播放图片的类(downmoon)新增图片效果
- C# 制作的WEB图片批量下载器
- php 批量下载图片的一个小程序
- 一个用于批量下载网络图片的Shell脚本
- 一个用于批量下载网络图片的Shell脚本
- 一个无限批量下载百度美女图片的demo
- php 批量下载图片的一个小程序
- 封装一个工具类,搞定图片批量下载
- 用C#开发一个WinForm版的批量图片压缩工具
- 用C#开发一个WinForm版的批量图片压缩工具
- 批量下载凡客诚品的图片
- 一个相当独立的.通用分页控件c#源码一(downmoon收集)
- 一个相当独立的.通用分页控件c#源码二(downmoon收集)
- BusyBox介绍
- [Linux可视化编程系列一] 用 Anjuta 轻松搭建 Gtkmm+libglademm 集成开发环境(IDE)
- 深入浅出之正则表达式(一)
- 深入浅出之正则表达式(二)
- ABAP 數據筆記 6-11-08
- 一个批量下载图片的c#类(downmoon)
- 有关SQL 截取和替换字符串
- 强制删除文件(1)——直接发IRP到文件系统
- 无法解决 equal to 操作的排序规则冲突
- 等待
- 常用正则表达式
- 深入分析MFC文档视图结构(项目实践)
- 无组件图片与文本同步存入数据库的最简单的办法
- 世界上最好的中文名字是什么