日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

使用HttpClient下载网络图片

發(fā)布時(shí)間:2023/12/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用HttpClient下载网络图片 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近公司業(yè)務(wù)需求,需要去XX網(wǎng)站爬取數(shù)據(jù),爬取速度過(guò)快時(shí),會(huì)導(dǎo)致當(dāng)前IP被封鎖,讓用戶輸入驗(yàn)證碼。目前使用OCR識(shí)別圖片驗(yàn)證碼并提交,故需要下載驗(yàn)證碼圖片,研究了一下終于給實(shí)現(xiàn)了。在這里分享一下,希望對(duì)大家有用!


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);}/*** 從網(wǎng)絡(luò)上下載圖片*/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();}}/*** 將圖片寫(xiě)到 硬盤指定目錄下* @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();}//文件真實(shí)路徑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,搞定啦、、、



總結(jié)

以上是生活随笔為你收集整理的使用HttpClient下载网络图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。