下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片 将图片的二进制字节字符串在HTML页面以图片形式输出 asp.net 文件 操作方法

下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片

 

将一台服务器的文件、图片,保存(下载)到另外一台服务器进行保存的方法:

复制代码
  1         #region 图片下载
  2 
  3         #region 图片下载【使用流、WebRequest进行保存】
  4         /// <summary>
  5         /// 图片下载【使用流、WebRequest进行保存】
  6         /// </summary>
  7         /// <param name="fileUrl">图片URL地址(例如:http://img.baidu.com/video/img/video_logo_new.gif) </param>
  8         /// <param name="path">存储到本地(服务器)路径(例如:Upload/Image)</param>
  9         /// <param name="fileName">文件名(包含后缀名)</param>
 10         /// <param name="fileFormat">图片格式</param>
 11         public static void WebRequestDownloadFileImage(string fileUrl, string path, string fileName, System.Drawing.Imaging.ImageFormat fileFormat)
 12         {
 13             try
 14             {
 15                 path = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", path));
 16                 if (!Directory.Exists(path))  //判断目录是否存在
 17                 {
 18                     Directory.CreateDirectory(path);//创建该文件
 19                 }
 20                 WebRequest wreq = WebRequest.Create(fileUrl);
 21                 using (HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse())
 22                 {
 23                     Stream s = wresp.GetResponseStream();
 24                     System.Drawing.Image img;
 25                     img = System.Drawing.Image.FromStream(s);
 26                     path = path + fileName;
 27                     img.Save(path, fileFormat);   //保存 
 28                 }
 29             }
 30             catch (Exception ex)
 31             {
 32                 throw;
 33             }
 34         }
 35         #endregion
 36 
 37         #region 图片下载【使用流、WebClient进行保存】
 38         /// <summary>
 39         /// 图片下载【使用流、WebClient进行保存】
 40         /// </summary>
 41         /// <param name="fileUrl">图片URL地址(例如:http://img.baidu.com/video/img/video_logo_new.gif) </param>
 42         /// <param name="path">存储到本地(服务器)路径(例如:Upload/Image)</param>
 43         /// <param name="fileName">文件名称(包含后缀名)</param>
 44         /// <param name="fileFormat">图片格式</param>
 45         public static void WebClientDownloadFileImage(string fileUrl, string path, string fileName, System.Drawing.Imaging.ImageFormat fileFormat)
 46         {
 47             try
 48             {
 49                 path = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", path));
 50                 if (!Directory.Exists(path))  //判断目录是否存在
 51                 {
 52                     Directory.CreateDirectory(path);//创建该文件
 53                 }
 54                 WebClient webClient = new WebClient();
 55                 byte[] imgByte;
 56                 imgByte = webClient.DownloadData(fileUrl);
 57                 using (MemoryStream ms = new MemoryStream(imgByte))
 58                 {
 59                     System.Drawing.Image img;
 60                     img = System.Drawing.Image.FromStream(ms);
 61                     path = path + fileName;
 62                     img.Save(path, fileFormat);   //保存 
 63                 }
 64             }
 65             catch (Exception ex)
 66             {
 67                 throw;
 68             }
 69 
 70         }
 71         #endregion
 72         #endregion
 73 
 74         #region 文件下载(从第三方服务器下载到本服务器)
 75 
 76         #region 下载(第三方)远程文件保存到本地(自己服务器)的方法、保存抓取远程图片【异步下载】
 77         /// <summary>
 78         /// 下载(第三方)远程图片保存到本地(自己服务器)的方法、保存抓取远程图片 【异步下载】
 79         /// </summary>
 80         /// <param name="fileUrl">文件URL地址(例如:http://img.baidu.com/video/img/video_logo_new.gif) </param>
 81         /// <param name="path">存储到本地(服务器)路径(例如:Upload/Image)</param>
 82         /// <param name="fileName">文件名称(包括后缀名)(例如:login.jpg)</param>
 83         public static void DownloadFileAsync(string fileUrl, string path, string fileName)
 84         {
 85             try
 86             {
 87                 System.Net.WebClient client = new System.Net.WebClient();
 88                 path = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", path));
 89                 if (!Directory.Exists(path))  //判断目录是否存在
 90                 {
 91                     Directory.CreateDirectory(path);//创建该文件
 92                 }
 93                 path = path + fileName;
 94                 client.DownloadFileAsync(new Uri(fileUrl, UriKind.RelativeOrAbsolute), path);
 95             }
 96             catch (Exception ex)
 97             {
 98 
 99             }
100         }
101         #endregion
102 
103         #region 下载(第三方)远程文件保存到本地(自己服务器)的方法、保存抓取远程图片【同步下载】
104         /// <summary>
105         /// 下载(第三方)远程图片保存到本地(自己服务器)的方法、保存抓取远程图片 【同步下载】
106         /// </summary>
107         /// <param name="fileUrl">文件URL地址(例如:http://img.baidu.com/video/img/video_logo_new.gif) </param>
108         /// <param name="path">存储到本地(服务器)路径(例如:Upload/Image)</param>
109         /// <param name="fileName">文件名称(包括后缀名)(例如:login.jpg)</param>
110         public static void DownloadFile(string fileUrl, string path, string fileName)
111         {
112             try
113             {
114                 System.Net.WebClient client = new System.Net.WebClient();
115                 path = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", path));
116                 if (!Directory.Exists(path))  //判断目录是否存在
117                 {
118                     Directory.CreateDirectory(path);//创建该文件
119                 }
120                 path = path + fileName;
121                 client.DownloadFile(new Uri(fileUrl, UriKind.RelativeOrAbsolute), path);
122             }
123             catch (Exception ex)
124             {
125 
126             }
127         }
128         #endregion
129 
130         #endregion
复制代码

 

将图片的二进制字节字符串在HTML页面以图片形式输出

 

具体实现代码如下:

1、新建一个一般处理程序: Image.ashx

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Net;
 6 using System.Drawing.Imaging;
 7 using System.IO;
 8 
 9 namespace Test
10 {
11     /// <summary>
12     ///测试图片以二进制字节输出到HTML页面(显示成图片)
13     /// </summary>
14     public class Image : IHttpHandler
15     {
16 
17         public void ProcessRequest(HttpContext context)
18         {
19             //方法一:
20             //WebRequest wreq = WebRequest.Create("http://img.baidu.com/video/img/video_logo_new.gif");
21             //HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
22             //Stream s = wresp.GetResponseStream();
23             //System.Drawing.Image img;
24             //img = System.Drawing.Image.FromStream(s);
25             ////下面直接输出
26             //MemoryStream ms = new MemoryStream();
27             //img.Dispose();
28 
29             //关键代码
30             //context.Response.ClearContent();
31             //context.Response.ContentType = "image/gif";
32             //context.Response.BinaryWrite(ms.ToArray());
33 
34             //方法二:
35             WebClient my = new WebClient();
36             byte[] mybyte;
37             mybyte = my.DownloadData("http://img.baidu.com/video/img/video_logo_new.gif"); 
38             MemoryStream ms = new MemoryStream(mybyte);
39             System.Drawing.Image img;
40             img = System.Drawing.Image.FromStream(ms);
41             //关键代码
42             context.Response.ClearContent();
43             context.Response.ContentType = "image/gif";
44             context.Response.BinaryWrite(mybyte); 
45         }
46 
47         public bool IsReusable
48         {
49             get
50             {
51                 return false;
52             }
53         }
54     }
55 }
复制代码

2、新建一个HTML页面:ImageTest.htm

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5 </head>
 6 <body>
 7 使用:Response.BinaryWrite 二进制字符串方式输出图片:<br />
 8 主要:图片 img标签路径(src)要指向那个(Image.ashx)一般处理程序<br />
 9 <img  src="Image.ashx"/>
10 </body>
11 </html>
复制代码

 

asp.net 文件 操作方法

 
复制代码
 1         /// <summary>
 2         /// 移动文件
 3         /// </summary>
 4         /// <param name="oldPath">源文件路径</param>
 5         /// <param name="newPath">目标文件路径</param>
 6         /// <param name="fileName">文件名称</param>
 7         public static void MoveFile(string oldPath, string newPath, string fileName)
 8         {
 9             if (!Directory.Exists(newPath))
10             {
11                 //不存在则自动创建文件夹
12                 Directory.CreateDirectory(newPath);
13             }
14             File.Move(oldPath + fileName, newPath + fileName);
15         }
16 
17         /// <summary>
18         /// 批量移动文件
19         /// </summary>
20         /// <param name="oldPath">源文件路径</param>
21         /// <param name="newPath">目标文件路径</param>
22         /// <param name="fileNameList">文件名称</param>
23         public static void MoveFile(string oldPath, string newPath, ArrayList fileNameList)
24         {
25             if (!Directory.Exists(newPath))
26             {
27                 //不存在则自动创建文件夹
28                 Directory.CreateDirectory(newPath);
29             }
30             for (int i = 0; i < fileNameList.Count; i++)
31             {
32                 File.Move(oldPath + fileNameList[i], newPath + fileNameList[i]);
33             }
34         }
35 
36         /// <summary>
37         /// 删除文件
38         /// </summary>
39         /// <param name="path">文件路径</param>
40         /// <returns>删除结果,成功或失败</returns>
41         public static bool DeleteFile(string path)
42         {
43             try
44             {
45                 File.Delete(path);
46                 return true;
47             }
48             catch
49             {
50                 return false;
51             }
52         }
53 
54         /// <summary>
55         /// 删除文件夹
56         /// </summary>
57         /// <param name="path">文件夹路径</param>
58         /// <returns>删除结果,成功或失败</returns>
59         public static bool DeleteFolder(string path)
60         {
61             try
62             {
63                 Directory.Delete(path);
64                 return true;
65             }
66             catch
67             {
68                 return false;
69             }
70         }
71 
72         /// <summary>
73         /// 移动文件夹
74         /// </summary>
75         /// <param name="oldPath">源文件夹路径</param>
76         /// <param name="newPath">目标文件夹路径</param>
77         /// <returns>移动结果</returns>
78         public static bool MoveFolder(string oldPath, string newPath)
79         {
80             try
81             {
82                 Directory.Move(oldPath, newPath);
83                 return true;
84             }
85             catch
86             {
87                 return false;
88             }
89         }    
复制代码

 

posted @ 2019-04-02 15:46  ~雨落忧伤~  阅读(424)  评论(0编辑  收藏  举报