使用HttpClient下载网络图片
2013-03-14 17:31
453 查看
最近公司业务需求,需要去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,搞定啦、、、
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,搞定啦、、、
相关文章推荐
- Afinal加载网络图片及下载文件使用方法
- 使用ListView和AsyncTask、fastjson解析Json以及适配器BaseAdapter来实现下载网络的图片以及文字并显示出来
- 使用http协议下载网络图片
- 使用httpclient下载 页面、图片
- 使用Okhttp网络请求下载图片到指定文件夹
- 在android中使用AsyncTask从网络中下载图片,但是调用了execute方法后,最后不执行onPostExcute方法
- 使用httpclient下载图片时,url中含有中文字符,导致下载失败的解决方法
- python 使用 urllib.urlretrieve()下载网络图片,在本地打开提示文件损坏无法打开
- 使用httpclient下载 页面、图片
- Android UI-开源框架ImageLoader完美使用+listview加载网络图片与下载
- 网络请求----简单框架使用------(百度图片下载实例)
- 使用org.jsoup.Jsoup下载网络中的图片
- 使用线程下载网络图片
- Android使用HttpClient加载网络数据(图片)
- 安卓多线程编程系列2:异步任务的使用之使用异步任务带有进度的横向滚动条下载网络图片
- Android学习篇章45-HttpClient网络下载-IO流传输-下载图片资源
- 使用Service服务下载网络图片并写入sdCard
- swift 使用Kingfisher进行网络图片的下载
- Java使用Http实现网络图片的下载
- Volley的初级使用(从网络下载图片)