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

 

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

  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         }    

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

  1. 请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出

    有些时候需要将二进制图片字节在发送浏览器以图片形式显示: 下面是一些示例代码: 控制器: /// <summary> /// 将图片的二进制字节字符串在视图页面以图片形式输出 /// &l ...

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

    具体实现代码如下: 1.新建一个一般处理程序: Image.ashx using System; using System.Collections.Generic; using System.Linq ...

  3. asp自动解析网页中的图片地址,并将其保存到本地服务器

    程序实现功能:自动将远程页面的文件中的图片下载到本地. 程序代码 <% '将本文保存为 save2local.asp '测试:save2local.asp?url=http://ent.sina ...

  4. 百度UEditor编辑器关闭抓取远程图片功能(默认开启)

    这个坑娘的功能,开始时居然不知道如何触发,以为有个按钮,点击一下触发,翻阅了文档,没有发现,然后再网络上看到原来是复制粘贴非白名单内的图片到编辑框时触发,坑娘啊............... 问题又来 ...

  5. PHP抓取远程图片教程(包含不带后缀图片)

    之前做微信登录开发时候,发现微信头像图片没有后缀名,传统的图片抓取方式不奏效,需要特殊的抓取处理.所以,后来将各种情况结合起来,封装成一个类,分享出来. 创建项目 作为演示,我们在www根目录创建项目 ...

  6. php抓取远程数据显示在下拉列表中

    前言:周五10月20日的时候,经理让做一个插件,使用的thinkphp做这个demo 使用CURL抓取远程数据时如果出现乱码问题可以加入 header("content-type:text/ ...

  7. 【转载】ASP.NET以Post方式抓取远程网页内容类似爬虫功能

    使用HttpWebRequest等Http相关类,可以在应用程序中或者网站中模拟浏览器发送Post请求,在请求带入相应的Post参数值,而后请求回远程网页信息.实现这一功能也很简单,主要是依靠Http ...

  8. php使用curl简单抓取远程url的方法

    这篇文章主要介绍了php使用curl简单抓取远程url的方法,涉及php操作curl的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了php使用curl抓取远程url的方法.分 ...

  9. 利用wireshark抓取远程linux上的数据包

    原文发表在我的博客主页,转载请注明出处. 前言 因为出差,前后准备总结了一周多,所以博客有所搁置.出差真是累人的活计,不过确实可以学习到很多东西,跟着老板学习做人,学习交流的技巧.入正题~ wires ...

随机推荐

  1. WinForm程序开发

    WinForm程序开发------------------------------主要页面----------------------------------BaseForm.cs    基类,用于派 ...

  2. Java通过ScriptEngine 执行js脚本案例

    public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodEx ...

  3. python程序打包

    环境: CentOS6.5_x64Python版本 : 2.6 使用pyinstaller打包 pyinstaller可以将python程序打包成二进制文件,打包后的文件在没有python的环境中也可 ...

  4. Android——gradle files have changed since last project sync

    这个报错,会引起锁死代码,不能编辑.不能编译等情况.    主要原因是:上一次修改gradle后,添加依赖失败.可能引进了过期.无效的依赖. 解决办法:把最近一次添加的依赖删除,重新编译项目.然后上网 ...

  5. 在vim中安装及配置NERDTree插件

    使用Vundle插件安装,在.vimrc中加入以下代码: Plugin 'scrooloose/nerdtree' 打开vim,输入命令如下: :BundleInstall 等待安装完毕 配置NERD ...

  6. SQL Server 默认跟踪(Default Trace)获取某个Trace跟踪了哪些Event和column

    检查Default Trace是否已经开启,如果返回Figure1中value为1,那就说明已经开启默认跟踪了:如果value为0表示关闭默认跟踪: --查询Default Trace是否开启 ; 如 ...

  7. cgroup 分析之CPU和内存部分

    https://ggaaooppeenngg.github.io/zh-CN/2017/05/07/cgroups-%E5%88%86%E6%9E%90%E4%B9%8B%E5%86%85%E5%AD ...

  8. spring & java 面试

    https://blog.csdn.net/u014079773/article/details/52453002 1.Spring中AOP的应用场景.Aop原理.好处? 答:AOP--Aspect ...

  9. 亲历H5移动端游戏微信支付接入及那些坑(一)——支付方式与坑

    最近项目进入中后期,开始接入支付.要求是使用微信支付,呵呵,好笑的是不知老板从哪里听来的,居然和我说只要是熟手,接个微信支付两小时搞定,我只能再次呵呵.先不说支付处理逻辑,而且公司本来也没现成的接入模 ...

  10. SpannableStringUtil实现丰富文字效果

    代码地址如下:http://www.demodashi.com/demo/15007.html 前言 在android开发中,我们不免会用到 TextView 的各种显示效果,如字体样式,颜色,大小, ...