成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

CloseableHttpClient出現(xiàn)PrematureendofContent-Lengthdelimitedmessagebody怎么解決

這篇文章主要講解了“CloseableHttpClient出現(xiàn)Premature end of Content-Length delimited message body怎么解決”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“CloseableHttpClient出現(xiàn)Premature end of Content-Length delimited message body怎么解決”吧!

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、羅江網(wǎng)絡(luò)推廣、重慶小程序開發(fā)公司、羅江網(wǎng)絡(luò)營銷、羅江企業(yè)策劃、羅江品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供羅江建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:jinyejixie.com

一、問題描述

    今天在用CloseableHttpClient請求服務(wù)器獲取圖片流,獲取之后轉(zhuǎn)換成字節(jié)數(shù)組,出現(xiàn)以下異常:

CloseableHttpClient出現(xiàn)Premature end of Content-Length delimited message body怎么解決

原代碼如下:

	public static byte[] getPic(String urlPath){
		try {
			InputStream inStream=null;
			/**
			 *
			 *方法二 HttpClient*/
			CloseableHttpClient client= HttpClients.createDefault();
			HttpGet method=new HttpGet(urlPath);//獲取內(nèi)容用GetMethod,經(jīng)過測試PostMethod拿不到圖片的內(nèi)容
//			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(DEFAULT_CONNECT_TIMEOUT)
//					.setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT)
//					.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT).build();
//			method.setConfig(requestConfig);
			CloseableHttpResponse response = client.execute(method);
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				byte[] btImg = EntityUtils.toByteArray(response.getEntity());
				return btImg;
			}else{
				log.error("網(wǎng)絡(luò)異常");
			}

		} catch (Exception e) {
			log.error("下載失敗");
			e.printStackTrace();
		}
		return null;
	}
	private static byte[] readStream(InputStream inStream) throws IOException {
		byte[] b = new byte[1024];
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		int l = -1;
		while((l = inStream.read(b)) > -1){
			os.write(b, 0, l);;
		}
		os.flush();
		b = os.toByteArray();
		os.close();
		return b;
	}

二、解決問題

    后來將CloseableHttpClient替換成了低版本的HttpClient,問題就解決了,暫時還不知道原因。

    修改后的代碼如下:

	public static byte[] getPic(String urlPath){
		try {
			InputStream inStream=null;
			/**
			 *
			 *方法二 HttpClient*/
			HttpClient client=new HttpClient();
			GetMethod method = new GetMethod(urlPath);//獲取內(nèi)容用GetMethod,經(jīng)過測試PostMethod拿不到圖片的內(nèi)容
			int status=client.executeMethod(method);
			method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8"); //設(shè)置編碼
			if(status==HttpStatus.SC_OK){
				inStream=method.getResponseBodyAsStream();
				//long a=System.currentTimeMillis();
				byte[] btImg = readStream(inStream);//得到圖片的二進制數(shù)據(jù)
				//long b=System.currentTimeMillis();
				//System.out.println("轉(zhuǎn)化為流耗時:"+(b-a));
				return btImg;
			}else{
				log.error("網(wǎng)絡(luò)異常");
			}

		} catch (Exception e) {
			log.error("下載失敗");
			e.printStackTrace();
		}
		return null;
	}
	private static byte[] readStream(InputStream inStream) throws IOException {
		byte[] b = new byte[1024];
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		int l = -1;
		while((l = inStream.read(b)) > -1){
			os.write(b, 0, l);;
		}
		os.flush();
		b = os.toByteArray();
		os.close();
		return b;
	}

感謝各位的閱讀,以上就是“CloseableHttpClient出現(xiàn)Premature end of Content-Length delimited message body怎么解決”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對CloseableHttpClient出現(xiàn)Premature end of Content-Length delimited message body怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

網(wǎng)頁題目:CloseableHttpClient出現(xiàn)PrematureendofContent-Lengthdelimitedmessagebody怎么解決
URL標題:http://jinyejixie.com/article32/gdgjpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、微信公眾號、網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站網(wǎng)站導(dǎo)航、網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司