如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]
Posted donaldlee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]相关的知识,希望对你有一定的参考价值。
http://bbs.csdn.net/topics/390426978
public static String readCheckImage(HashMap<String, String> requestMap) {
String url = requestMap.get("url");
if (requestMap.get("params") != null) {
url += "?" + requestMap.get("params");
}
InputStream in = null;
try {
System.out.println("get>>>" + url);
URL serverUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();
conn.setRequestMethod("GET");// "POST" ,"GET"
conn.addRequestProperty("Cookie", requestMap.get("cookie"));
conn.addRequestProperty("Accept-Charset", "UTF-8;");// GB2312,
conn.addRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
conn.connect();
// 判断请求是否成功
if (((HttpURLConnection) conn).getResponseCode() == 200) {
// 获取所有响应头字段
Map<String, List<String>> map = conn.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 返回的cookie
String cookie = null;
if (conn.getHeaderFields().get("Set-Cookie") != null) {
for (String s : conn.getHeaderFields().get("Set-Cookie")) {
cookie += s;
}
}
requestMap.put("cookie", requestMap.get("cookie") + cookie);
in = conn.getInputStream();
BufferedImage bi = ImageIO.read(in);
File f = new File("check_img.jpg");
ImageIO.write(bi, "jpg", f);
return f.getAbsolutePath();
} else {
System.out.println("下载code图片失败!");
}
} catch (Exception e) {
e.printStackTrace();
}// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
以上是关于如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]的主要内容,如果未能解决你的问题,请参考以下文章