下载远程指定路径URL图片
2015-09-02 09:23
337 查看
public static void DownLoadFile(string Url, string FileName) { bool Value = false; WebResponse response = null; Stream stream = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); response = request.GetResponse(); stream = response.GetResponseStream(); if (!response.ContentType.ToLower().StartsWith("text/")) { Value = SaveBinaryFile(response, FileName); } } catch (Exception err) { string aa = err.ToString(); } } /// <summary> /// Save a binary file to disk. /// </summary> /// <param name="response">The response used to save the file</param> // 将二进制文件保存到磁盘 private static bool SaveBinaryFile(WebResponse response, string FileName) { bool Value = true; byte[] buffer = new byte[1024]; try { if (File.Exists(FileName)) File.Delete(FileName); Stream outStream = System.IO.File.Create(FileName); Stream inStream = response.GetResponseStream(); int l; do { l = inStream.Read(buffer, 0, buffer.Length); if (l > 0) outStream.Write(buffer, 0, l); } while (l > 0); outStream.Close(); inStream.Close(); } catch { Value = false; } return Value; }
相关文章推荐
- LoadRunner11学习记录六
- Tomcat-8.0.26报错,这是个坑大家不要在踩了
- NGUI中深度depth和z轴关系
- Java____继承与组合
- js不加alert后面的代码不工作
- 盘古分词词性
- 乒乓网开始制作进击的格林卡系列
- 头文件
- Activiti挂起与激活
- 栈
- jQuery的ajax报错:SyntaxError: missing ] after element list
- Socket通信---网络通信学习笔记(一)
- The Business Versus the Angry Architect
- Android中级篇之百度地图SDK v3.5.0-百度地图定位[超详细图解定位基础]
- mysql中set autocommit=0与start transaction区别
- UVa 1629 DP Cake slicing
- dubbo使用简单说明
- BCB 报错 BORLNDMM.DLL has already allcated memory
- ubuntu 文件夹内打开终端的方法
- GridView内存泄露以及显示自定义类型的资源