這篇文章主要講解了“CloseableHttpClient出現(xiàn)Premature end of Content-Length delimited message body怎么解決”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“CloseableHttpClient出現(xiàn)Premature end of Content-Length delimited message body怎么解決”吧!
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于成都網站設計、成都網站制作、外貿網站建設、羅江網絡推廣、重慶小程序開發(fā)公司、羅江網絡營銷、羅江企業(yè)策劃、羅江品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供羅江建站搭建服務,24小時服務熱線:13518219792,官方網址:chinadenli.net
一、問題描述
今天在用CloseableHttpClient請求服務器獲取圖片流,獲取之后轉換成字節(jié)數(shù)組,出現(xiàn)以下異常:

原代碼如下:
public static byte[] getPic(String urlPath){
try {
InputStream inStream=null;
/**
*
*方法二 HttpClient*/
CloseableHttpClient client= HttpClients.createDefault();
HttpGet method=new HttpGet(urlPath);//獲取內容用GetMethod,經過測試PostMethod拿不到圖片的內容
// 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("網絡異常");
}
} 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);//獲取內容用GetMethod,經過測試PostMethod拿不到圖片的內容
int status=client.executeMethod(method);
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8"); //設置編碼
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("轉化為流耗時:"+(b-a));
return btImg;
}else{
log.error("網絡異常");
}
} 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怎么解決”的內容了,經過本文的學習后,相信大家對CloseableHttpClient出現(xiàn)Premature end of Content-Length delimited message body怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!
網頁題目:CloseableHttpClient出現(xiàn)PrematureendofContent-Lengthdelimitedmessagebody怎么解決
URL標題:http://chinadenli.net/article32/gdgjpc.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供App設計、微信公眾號、網站內鏈、響應式網站、網站導航、網站維護
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)