使用httpclient下載 頁面、圖片
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("返回的狀態碼:" + 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("); 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(); } } }
瞭解springcloud架構可以加求求:一零三八七七四六二六
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69952307/viewspace-2677489/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 前端js儲存頁面為圖片下載到本地前端JS
- 頁面圖片預載入與懶載入策略
- python 爬蟲之requests爬取頁面圖片的url,並將圖片下載到本地Python爬蟲
- 小米手機載入h5頁面載入不出圖片H5
- Java爬蟲系列二:使用HttpClient抓取頁面HTMLJava爬蟲HTTPclientHTML
- 頁面圖片自動滾動
- HttpClient 下載檔案HTTPclient
- 求助:關於ofbiz頁面無法載入圖片問題
- html2canvas.js實現前端將頁面轉化為圖片並長按下載HTMLCanvasJS前端
- 圖片下載框架概述框架
- Python 下載圖片Python
- 使用html2canvas將頁面轉成圖,用canvas2image下載HTMLCanvas
- 用canvas生成圖片為頁面新增水印Canvas
- html頁面轉PDF、圖片操作記錄HTML
- 使用Web元件載入頁面Web元件
- 使用Python實現網頁中圖片的批次下載和水印新增儲存Python網頁
- 本地HTML中圖片下載HTML
- 仿SDWebImage多圖片下載Web
- android Gallery實現非同步載入網路圖片 並只載入當前停止頁面圖Android非同步
- WebClient和HttpClient, 以及webapi上傳圖片WebclientHTTPAPI
- 使用 canvas 繪製圖片,然後下載、上傳Canvas
- python中使用urllib下載網站圖片Python網站
- 用正規表示式自動下載網頁中的圖片網頁
- 記一次ios下h5頁面圖片顯示問題iOSH5
- 實現java讀取網頁內容並下載網頁中出現的圖片Java網頁
- python自動下載圖片Python
- Opencv官方樣例圖片下載OpenCV
- 獲取SDWebImage下載的圖片Web
- HTML使用canvas載入圖片HTMLCanvas
- 使用Python爬蟲實現自動下載圖片Python爬蟲
- 使用ABAP批量下載有道雲筆記中的圖片筆記
- Android 使用WebView載入含有Canvas的頁面截圖處理AndroidWebViewCanvas
- TestFlight下載App,載入圖片失效。Xcode安裝App,圖片載入正常。APPXCode
- Java後臺Html轉圖片和獲取頁面屬性值,及圖片拼接JavaHTML
- springMVC下載模板檔案(不跳頁面)SpringMVC
- 微信H5頁儲存當前頁面為圖片踩坑H5
- AlamofireImage 使用 – swift載入網路圖片,縮放圖片,生成圓形圖片Swift
- 前端實現點選下載圖片前端