使用Jetty Http Client 接收较大数据时发生异常
Request request = client.POST(api_url)
.timeout(TIMEOUT, TimeUnit.SECONDS)
.content(new BytesContentProvider(imageData), "image/*");ContentResponse resp = request.send();
if(resp.getStatus()==200){
request.getContent();
}
send 函数抛出如下异常:
java.lang.IllegalArgumentException: Buffering capacity 2097152 exceeded
后分析发现,再接收较大数据时,超出内部buffer,进而产生异常,解决方法,使用InputStreamResponseListener 来读取数据。
Request request = client.POST(api_url)
.timeout(TIMEOUT, TimeUnit.SECONDS)
.content(new BytesContentProvider(imageData), "image/*");InputStreamResponseListener listener = new InputStreamResponseListener();
request.send(listener);
HttpResponse resp = (HttpResponse) listener.get(12,TimeUnit.SECONDS);if(resp.getStatus()==200){
InputStream input = listener.getInputStream()
// read data from input.
}