使用HttpClient下载网络图片
最近公司业务需求,需要去XX网站爬取数据,爬取速度过快时,会导致当前IP被封锁,让用户输入验证码。目前使用OCR识别图片验证码并提交,故需要下载验证码图片,研究了一下终于给实现了。在这里分享一下,希望对大家有用!
DownloadPictureTest类
package com.yulore.checkcode;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;/*** 下载图片工具类* * @author bingbing feng 2013-03-14* */
public class DownloadPictureTest {/*** @param args*/public static void main(String[] args) {String uid = "871170f2-2598-48e5-9ee8-58ed6379d8931d2ec8";String s = "1363239309732";String fileName = "ex2.png";getCheckCodePicFromXX(uid,s,fileName);}private static void getCheckCodePicFromXX(String uid, String s,String fileName) {String url = "http://wap.xxx.com//p/ex.d?u_id="+uid+"&m=gvcd&s="+s;String dirPath = "D:/OCR_EX/";downloadPicture(url, dirPath, fileName);}/*** 从网络上下载图片*/public static void downloadPicture(String url, String dirPath,String filePath) {DefaultHttpClient httpclient = new DefaultHttpClient();HttpGet httpget = new HttpGet(url);httpget.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1");httpget.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");try {HttpResponse resp = httpclient.execute(httpget);if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) {HttpEntity entity = resp.getEntity();InputStream in = entity.getContent();savePicToDisk(in, dirPath, filePath);System.out.println("保存图片 "+filePath+" 成功....");}} catch (Exception e) {e.printStackTrace();} finally {httpclient.getConnectionManager().shutdown();}}/*** 将图片写到 硬盘指定目录下* @param in* @param dirPath* @param filePath*/private static void savePicToDisk(InputStream in, String dirPath,String filePath) {try {File dir = new File(dirPath);if (dir == null || !dir.exists()) {dir.mkdirs();}//文件真实路径String realPath = dirPath.concat(filePath);File file = new File(realPath);if (file == null || !file.exists()) {file.createNewFile();}FileOutputStream fos = new FileOutputStream(file);byte[] buf = new byte[1024];int len = 0;while ((len = in.read(buf)) != -1) {fos.write(buf, 0, len);}fos.flush();fos.close();} catch (IOException e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}}}
OK,搞定啦、、、
使用HttpClient下载网络图片相关推荐
- ASP.NET下载网络图片
直接上干货: 目录 获取页面中的图片或者MP3文件路径 保存网路图片 保存网络文字 获取页面中的图片或者MP3文件路径 /// <summary>/// 获取网页中的图片路径list集合/ ...
- 安卓Service组件使用系列2:使用Service下载网络图片并存储于sdCard卡上
使用启动式Service的方式可以处理网络的数据交互.音乐播放.执行IO操作(这些操作都是来自后台的).下面我们以下载网络图片并存储于sdCard卡为实际应用背景来说明它的使用方法. 整体思路:在xm ...
- 安卓Service组件使用系列3:使用IntentService下载网络图片
使用IntentService的特点: 1.不需要开启线程 2.不需要关闭服务,自己会关闭 3.使用单线程下载数据 (不需要完成线程的编写,直接访问网络就可以获取图片) 因为以上两点都在 ...
- Android异步下载网络图片(其三:ExecutorService)
在第一节中,我们使用Handler.Thread/Runnable .URL.HttpURLConnection等等来进行异步下载网络图片.然后第二节中换了AsyncTask方式.那么这一节我们再应用 ...
- Android异步下载网络图片(其二:AsyncTask)
在上一节中,我们使用Handler.Thread/Runnable .URL.HttpURLConnection等等来进行异步下载网络图片. 但是采用这种方式有一些缺点,如下: 线程的开销较大,如果每 ...
- Android开发-下载网络图片并显示到本地
Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件. 注意点 最新的Android系统不可以在主线程上请求 ...
- IOS开发基础之异步下载网络图片第1部分
IOS开发基础之异步下载网络图片第1部分 加入ATS // LJAppInfo.h // 37-异步下载网络图片 // Created by 鲁军 on 2021/3/10. #import < ...
- Python下载网络图片方法汇总与实现
本文介绍下载python下载网络图片的方法,包括通过图片url直接下载.通过re/beautifulSoup解析html下载以及对动态网页的处理等. 很多人学习python,不知道从何学起. 很多人 ...
- 安卓案例:基于HttpClient下载文本与图片
安卓案例:基于HttpClient下载文本与图片 一.利用HttpClient访问网络资源 1.创建http请求(get方式.post方式) 2.创建http客户端
- 使用php下载网络图片有哪些方法,php下载网络图片常用的三个方法总结
下面来讲利用PHP下载网络图片常用的三个方法.当然,这三个方法都不是万能的,有时候会因为网络问题,或者网络图片的权限问题(大站一般都有防盗链)导致下载失败. 一起来看看吧,实际工作中经常用到. 方法一 ...
最新文章
- Ti公司DSP的DSK、EVM、DDK的区别
- pip快速下载安装python 模块module
- #{}不自动改参数类型_ORT-260电动打包机常规参数
- 什么插件格式化文档_推荐15款IntelliJ IDEA 神级插件
- 用C#开发.NET CF 蓝牙通信模块
- js对象(Object)和字符串(String)互转 - 代码篇
- SQL SERVER 2012 执行计划走嵌套循环导致性能问题的案例
- susue linux防火墙,在64位的SUSUE下,g++编译的代码中,进入函数前的一段汇编让人费解,请高手解释一下?...
- 管理感悟:工作计划的作用
- 如何学习32位单片机
- 项目实战之电子商城数据库源码
- python 标贝 模拟人声/语音克隆/语音复刻(API)体验
- 咨询案例:再来几种利益相关人地图
- beautifulsoup函数
- 从零开始搭建本地mongodb环境
- c语言短信猫开发代码
- 5.0 DataView使用说明
- 超全面的JavaWeb笔记day01HTML等
- 云队友丨外卖骑手,巨头的炮灰
- CF915C Permute Digits(搜索+剪枝)
热门文章
- 高响应比优先算法实例
- 天正cad计算机快捷键,CAD、天正快捷键(专用)
- html css 书签,CSS实现书签图案的效果
- ctypes 传递opencv二值化图像给C++函数
- mysql查询记录总数_MySQL记录总条数实现查询优化
- 安装 | 最新MATLAB 2020b(64位)安装教程完整版
- vim字符串全局替换
- 三星note9刷android11,【极光ROM】-【三星NOTE9 N960X-845】-【V17.0 Android-Q-TG1】
- 《文后参考文献著录规则》
- windows10华硕安装杜比音效