/// <summary>
/// 下载图片
/// </summary>
/// <param name="picUrl">图片Http地址</param>
/// <param name="savePath">保存路径</param>
/// <param name="timeOut">Request最大请求时间,如果为-1则无限制</param>
/// <returns></returns>
public bool DownloadPicture(string picUrl, string savePath, int timeOut)
{
bool value = false;
WebResponse response = null;
Stream stream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(picUrl);
if (timeOut != -1) request.Timeout = timeOut;
response = request.GetResponse();
stream = response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith("text/"))
value = SaveBinaryFile(response, savePath);
}
finally
{
if (stream != null) stream.Close();
if (response != null) response.Close();
}
return value;
}
private static bool SaveBinaryFile(WebResponse response, string savePath)
{
bool value = false;
byte[] buffer = new byte[1024];
Stream outStream = null;
Stream inStream = null;
try
{         string dic = Path.GetDirectoryName(savePath);                if (string.IsNullOrEmpty(dic)) return false;                if (Directory.Exists(dic) == false) Directory.CreateDirectory(dic);
if (File.Exists(savePath)) File.Delete(savePath);
outStream = System.IO.File.Create(savePath);
inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0) outStream.Write(buffer, 0, l);
} while (l > 0);
value = true;
}
finally
{
if (outStream != null) outStream.Close();
if (inStream != null) inStream.Close();
}
return value;
}

根据URL下载图片到本地的更多相关文章

  1. QT通过url下载图片到本地

    /* strUrl:下载图片时需要的url strFilePath:下载图片的位置(/home/XXX/YYY.png) */ void ThorPromote::downloadFileFromUr ...

  2. python实现通过URL下载图片到本地服务器

    import os import urllib.request image_url = 'http://img.jingtuitui.com/759fa20190115144450401.jpg' f ...

  3. JAVA 通过url下载图片保存到本地

    //java 通过url下载图片保存到本地 public static void download(String urlString, int i) throws Exception { // 构造U ...

  4. java根据图片的url地址下载图片到本地

    package com.daojia.haobo.aicircle.util; import sun.misc.BASE64Encoder; import java.io.*; import java ...

  5. php下载图片到本地

    写了一天,就写了这么点代码,凑合用吧. #saveImage.php<?php /** * 图片下载方法,提供两种图片保存方式: * 1.按照图片自带的名称保存 * 2.按照自定义文件名保存 * ...

  6. 通过HttpURLConnection下载图片到本地--下载附件

    一.背景说明 现在我做的系统中,需要有一个下载附件的功能,其实就是下载图片到本地中.相应的图片保存在多媒体系统中,我们只能拿到它的资源地址(url),而不是真实的文件. 这里记录的是下载单个图片.下篇 ...

  7. C++根据图片url下载图片

    需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明. URLDownloadToFile()函数的定义如下: HRESULT URLDownlo ...

  8. 从url下载图片--java与python实现方式比较

    从url下载图片--java与python实现方式比较 博客分类: 技术笔记小点滴 javapython图片下载  一.java的实现方式 首先读取图片 //方式一:直接根据url读取图片 priva ...

  9. java从网络中下载图片到本地

    public class imageDownload { public static void main(String[] args) { String url = "http://loca ...

随机推荐

  1. Python--函数&过程

    函数式编程与过程式编程打的区分:过程是没有返回值的函数,过程在python3中也有返回值,为None 函数的作用:代码复用.保持代码的一致性.使代码更容易扩展 过程的定义与调用: 1 def func ...

  2. 描述一下 JVM 加载 class 文 件的原理机制?

    JVM 中类的装载是由 ClassLoader 和它的子类来实现的, Java ClassLoader 是一个重要的 Java 运行时系统组件.它负责在运行时查找和装入类文件的类.

  3. [NOI Online #3]魔法值

    题目   点这里看题目. 分析   我们不难想到,对于系数进行一下的拆分: \[\begin{aligned} f(u,j)&=\bigoplus_{(u,v)\in E} f(v,j-1)\ ...

  4. Python爬虫小白入门(七)爬取豆瓣音乐top250

      抓取目标: 豆瓣音乐top250的歌名.作者(专辑).评分和歌曲链接 使用工具: requests + lxml + xpath. 我认为这种工具组合是最适合初学者的,requests比pytho ...

  5. Linux 虚拟机详细安装MySQL

    准备工作 下载MySQL 去官网下载MySQL:点我直达 百度云盘地址:链接: https://pan.baidu.com/s/1qBN4r6t8gvq-I4CFfQQ-EA 密码: hei3 检查L ...

  6. c++_primer_第4版目录

    https://vdisk.weibo.com/s/BN_NALmbbBH01 第1章 快速入门1.1 编写简单的C++程序1.2 初窥输入/输出1.2.1 标准输入与输出对象1.2.2 一个使用IO ...

  7. GeckoDriver+Selenium+Python的安装和使用

    如果没有安装GeckoDriver会提示: selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executab ...

  8. Selenium自动化测试与练习

    Selenium WebDriver 提供了web自动化各种语言(java python ruby等等) 调用接口库 提供 各种浏览器的驱动(web driver) 来驱动浏览器的 特点 测试程度可以 ...

  9. talonspilder的的提问

    有个问题:我的代码是: [1]summary=TextField(css_select="#intro>p") [2]def tal_summary(self,summary ...

  10. skywalking7 源码解析 (3) :agent启动服务分析以及性能影响

    skywalking必看的文章,转载自https://blog.csdn.net/u010928589/article/details/106608864/