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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

使用httpclient下载 页面、图片

發(fā)布時間:2023/12/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用httpclient下载 页面、图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

代碼

import java.io.IOException; import java.io.UnsupportedEncodingException;import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;public class MyHttpClientUtils {private static Logger logger = LoggerFactory.getLogger(MyHttpClientUtils.class);private static final int HTTPCLIENT_TIMEOUT = 5000;public static Tuple2<Boolean,String> getPageByUrl(String pageUrl, String encode) throws UnsupportedEncodingException{logger.info("pageurl=" + pageUrl);String body = null;RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTPCLIENT_TIMEOUT) //socket超時.setConnectTimeout(HTTPCLIENT_TIMEOUT) //connect超時 .build();CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();HttpGet httpGet = new HttpGet(pageUrl);try {CloseableHttpResponse response = httpClient.execute(httpGet); String statusCode = String.valueOf(response.getStatusLine().getStatusCode());logger.info("getStatusCode=" + response.getStatusLine().getStatusCode());if(response.getStatusLine().getStatusCode() != 200) {logger.info("返回碼異常:" + response.getStatusLine().getStatusCode());return new Tuple2<Boolean, String>(false, null);}body = EntityUtils.toString(response.getEntity(), encode); // System.out.println("body=" + body);} catch (IOException e) {System.out.println("----------Connection timeout--------"); // return ne }return new Tuple2<Boolean, String>(true, body);}public static Tuple2<Boolean,byte[]> getPicByteArray(String picUrl) throws ClientProtocolException, IOException {logger.info("下載url=" + picUrl);RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectionRequestTimeout(HTTPCLIENT_TIMEOUT).setConnectTimeout(HTTPCLIENT_TIMEOUT).build();CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();HttpGet httpGet = new HttpGet(picUrl);CloseableHttpResponse response = httpClient.execute(httpGet);logger.info("返回的狀態(tài)碼:" + response.getStatusLine().getStatusCode());if(response.getStatusLine().getStatusCode() == 200) {byte[] bytes = EntityUtils.toByteArray(response.getEntity());return new Tuple2<>(true, bytes);}else{return new Tuple2<>(false, null);}} } package testGetpic;import java.io.FileOutputStream; import java.io.IOException;import org.apache.http.Header; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;public class TestPic2 {public static void main(String[] args) throws IOException{RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectionRequestTimeout(6000).setConnectTimeout(6000).build();CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();HttpGet httpGet = new HttpGet("http://xxxx.com/abc.jpg");CloseableHttpResponse response = httpClient.execute(httpGet);Header[] headerArray = response.getAllHeaders();for(Header h : headerArray) {System.out.println(h.getName());System.out.println(h.getValue());System.out.println("======");}System.out.println("---------------");System.out.println(response.getStatusLine().getStatusCode());if(response.getStatusLine().getStatusCode() == 200) {// for(Header h :response.getAllHeaders()){ // System.out.println(h.getElements().length); // for(HeaderElement he :h.getElements()){ // System.out.println("pc=" + he.getParameterCount()); // } // System.out.println( h.getName() ); // System.out.println( h.getValue() ); // }byte[] b = EntityUtils.toByteArray(response.getEntity());FileOutputStream fos = new FileOutputStream("test2.jpg");fos.write(b);fos.close();}}}

?

總結(jié)

以上是生活随笔為你收集整理的使用httpclient下载 页面、图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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