资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

CloseableHttpClient出现PrematureendofContent-Lengthdelimitedmessagebody怎么解决

这篇文章主要讲解了“CloseableHttpClient出现Premature end of Content-Length delimited message body怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“CloseableHttpClient出现Premature end of Content-Length delimited message body怎么解决”吧!

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都网站制作、外贸网站建设、罗江网络推广、重庆小程序开发公司、罗江网络营销、罗江企业策划、罗江品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供罗江建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

一、问题描述

    今天在用CloseableHttpClient请求服务器获取图片流,获取之后转换成字节数组,出现以下异常:

CloseableHttpClient出现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);//获取内容用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);//得到图片的二进制数据
				//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出现Premature end of Content-Length delimited message body怎么解决”的内容了,经过本文的学习后,相信大家对CloseableHttpClient出现Premature end of Content-Length delimited message body怎么解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


当前题目:CloseableHttpClient出现PrematureendofContent-Lengthdelimitedmessagebody怎么解决
转载源于:http://cdkjz.cn/article/gdgjpc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220