国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

抓取服務(wù)器圖片下載到本地

2019-11-15 00:44:48
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
抓取服務(wù)器圖片下載到本地Sample code:import org.apache.http.Header;import org.apache.http.HttPResponse;import org.apache.http.HttpStatus;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;public void download(String path, String url) { // String url = "xxxxxxxx"; // String path = "F:/test.jpg"; HttpClient client = null; try { // 創(chuàng)建HttpClient對(duì)象 client = new DefaultHttpClient(); // 獲得HttpGet對(duì)象 HttpGet httpGet = getHttpGet(url, null, null); // 發(fā)送請(qǐng)求獲得返回結(jié)果 HttpResponse response = client.execute(httpGet); // 如果成功 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { byte[] result = EntityUtils.toByteArray(response.getEntity()); BufferedOutputStream bw = null; try { // 創(chuàng)建文件對(duì)象 File f = new File(path); // 創(chuàng)建文件路徑 if (!f.getParentFile().exists()) f.getParentFile().mkdirs(); // 寫(xiě)入文件 bw = new BufferedOutputStream(new FileOutputStream(path)); bw.write(result); } catch (Exception e) { log.error("保存文件錯(cuò)誤,path=" + path + ",url=" + url, e); } finally { try { if (bw != null) bw.close(); } catch (Exception e) { log.error( "finally BufferedOutputStream shutdown close", e); } } } // 如果失敗 else { StringBuffer errorMsg = new StringBuffer(); errorMsg.append("httpStatus:"); errorMsg.append(response.getStatusLine().getStatusCode()); errorMsg.append(response.getStatusLine().getReasonPhrase()); errorMsg.append(", Header: "); Header[] headers = response.getAllHeaders(); for (Header header : headers) { errorMsg.append(header.getName()); errorMsg.append(":"); errorMsg.append(header.getValue()); } log.error("HttpResonse Error:" + errorMsg); } } catch (ClientProtocolException e) { log.error("下載文件保存到本地,http連接異常,path=" + path + ",url=" + url, e); } catch (IOException e) { log.error("下載文件保存到本地,文件操作異常,path=" + path + ",url=" + url, e); } finally { try { client.getConnectionManager().shutdown(); } catch (Exception e) { log.error("finally HttpClient shutdown error", e); } } } private static HttpGet getHttpGet(String url, Map<String, String> params, String encode) { StringBuffer buf = new StringBuffer(url); if (params != null) { // 地址增加?或者& String flag = (url.indexOf('?') == -1) ? "?" : "&"; // 添加參數(shù) for (String name : params.keySet()) { buf.append(flag); buf.append(name); buf.append("="); try { String param = params.get(name); if (param == null) { param = ""; } buf.append(URLEncoder.encode(param, encode)); } catch (UnsupportedEncodingException e) { log.error("URLEncoder Error,encode=" + encode + ",param=" + params.get(name), e); } flag = "&"; } } HttpGet httpGet = new HttpGet(buf.toString()); return httpGet; }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 化州市| 瑞安市| 霍山县| 安国市| 白银市| 甘肃省| 灵璧县| 库车县| 重庆市| 应城市| 贵溪市| 江油市| 甘肃省| 堆龙德庆县| 焉耆| 镇远县| 三江| 八宿县| 布尔津县| 定远县| 泰顺县| 南城县| 穆棱市| 读书| 合江县| 房山区| 和平县| 顺平县| 永泰县| 洛浦县| 湖南省| 会泽县| 玉树县| 定州市| 兖州市| 佛坪县| 苍梧县| 富锦市| 措勤县| 柘荣县| 依安县|