您的位置:首页 > 理论基础 > 计算机网络

使用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,搞定啦、、、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息