第一节: HttpClient 抓取图片

这里pom.xml需要用到io输入输出:

     <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

pom.xml 文件:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javaxk</groupId>
<artifactId>HttpClientTest</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency> <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency> </dependencies> </project>
 package com.javaxk.httpclient.chap03;

 import java.io.File;
import java.io.InputStream; import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; public class Demo1 { public static void main(String[] args)throws Exception {
CloseableHttpClient httpClient=HttpClients.createDefault(); // 创建httpClient实例
HttpGet httpGet=new HttpGet("http://www.javaxk.com/templets/javaxk/images/logo.jpg"); // 创建httpget实例
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
CloseableHttpResponse response=httpClient.execute(httpGet); // 执行http get请求
HttpEntity entity=response.getEntity(); // 获取返回实体
if(entity!=null){
System.out.println("ContentType:"+entity.getContentType().getValue());
InputStream inputStream=entity.getContent();
FileUtils.copyToFile(inputStream, new File("D://logo.jpg"));
}
response.close(); // response关闭
httpClient.close(); // httpClient关闭
} }

运行输出:

ContentType:image/jpeg

D盘下会有一个logo.jpg的图片

(三)HttpClient 抓取图片的更多相关文章

  1. HttpClient(三)-- 抓取图片

    使用HttpClient抓取图片,先通过 entity.getContent() 获取输入流,然后 使用 common io 中的文件复制 方法 将图片专区到本地,代码如下: 1.需要依赖common ...

  2. HttpClient(一)HttpClient抓取网页基本信息

    一.HttpClient简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包, 并且它支 ...

  3. ffmpeg 从视频流中抓取图片

    从视频中不断抓取图片的基本流程:打开视频流地址->获取视频流packt->解码成图片帧->输出图片 一.初始化Ffmpeg void ffmpegInit(){ av_registe ...

  4. Phantomjs+Nodejs+Mysql数据抓取(2.抓取图片)

    概要 这篇博客是在上一篇博客Phantomjs+Nodejs+Mysql数据抓取(1.抓取数据) http://blog.csdn.net/jokerkon/article/details/50868 ...

  5. HTTPCLIENT抓取网页内容

    通过httpclient抓取网页信息. public class SnippetHtml{ /** * 通过url获取网站html * @param url 网站url */ public Strin ...

  6. python3用BeautifulSoup抓取图片地址

    # -*- coding:utf-8 -*- #python 2.7 #XiaoDeng #http://tieba.baidu.com/p/2460150866 #抓取图片地址 from bs4 i ...

  7. curl 抓取图片

    /** * curl 抓取图片 * @param $url * @return mixed */ public static function downLoadImage($url) { $heade ...

  8. 【python】网络爬虫抓取图片

    利用python抓取网络图片的步骤: 1.根据给定的网址获取网页源代码 2.利用正则表达式把源代码中的图片地址过滤出来 3.根据过滤出来的图片地址下载网络图片 今天我们用http://www.umei ...

  9. HttpClient抓取网页内容简单介绍

    版本HttpClient3.1 1.GET方式 第一步.创建一个客户端,类似于你用浏览器打开一个网页 HttpClient httpClient = new HttpClient(); 第二步.创建一 ...

随机推荐

  1. bzoj 3667 Rabin-Miller算法

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...

  2. postgresql 数据库无法启动

    在数据库无法启动时,一般可以根据报错信息,采取对应措施即可,下面列出一些在数据库启动时报出错误比较严重而解决方式又不那么明显的处理方法. 模拟错误,查到pg_class系统表中一个索引在磁盘中的位置, ...

  3. 网络优化之net.ipv4.tcp_tw_recycle参数

    不要在linux上启用net.ipv4.tcp_tw_recycle参数  2015/07/27  CFC4N 本文为翻译英文BLOG<Coping with the TCP TIME-WAIT ...

  4. Service Fabric Cluster Manager

    作者:潘罡 (Van Pan)@ Microsoft 我们回到Service Fabric最底层的话题,谈谈Service Fabric是怎么工作的. 首先,我们回到下面的文档,看看Service F ...

  5. Hadoop生态圈-使用MapReduce处理HBase数据

    Hadoop生态圈-使用MapReduce处理HBase数据 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.对HBase表中数据进行单词统计(TableInputFormat) ...

  6. day15 接口与异常

    接口 是一种独立于类的新机制,它关注的是行为. 接口的意义就体现在——让没有继承关系的类共享这些行为,各有各的具体实现. 设计上:当我们发现一个或多个类的方法相同,又没有继承关系,则考虑接口. 命名: ...

  7. HTTP协议(2)-------- 网络编程

    1. HTTP请求格式 做过Socket编程的人都知道,当我们设计一个通信协议时,“消息头/消息体”的分割方式是很常用的,消息头告诉对方这个消息是干什么的,消息体告诉对方怎么干.HTTP协议传输的消息 ...

  8. 防止jquery ajax 重复提交

    var requestSent = false; jQuery("#buttonID").click(function() { if(!requestSent) { request ...

  9. hbase系列之:初识hbase

    一.概述 在hadoop生态圈里,hbase可谓是鼎鼎大名.江湖传言,hbase可以实现数十亿行X数百万列的实时查询,可横向扩展存储空间.如果传言为真,那得好好了解了解hbase.本文从概念上介绍hb ...

  10. Overfitting & Regularization

    Overfitting & Regularization The Problem of overfitting A common issue in machine learning or ma ...