问题
I am trying to upload file to an url, and have received instead of regular JSON string response, only ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}
. As I understood, there is JSON string as a response in there and I need to extract it somehow.
Here is my code what I have tried so far to do (this is in the method where I am uploading file to an url):
public String uploadDocument() {
String responseMsg="empty";
try(CloseableHttpClient client = HttpClients.createDefault()){
HttpPost httpPost = new HttpPost(SupportUtil.WHATSAPP_PUSH_BASEURL+"/media/upload/");
httpPost.addHeader("Content-type", "application/octet-stream");
httpPost.addHeader("Authorization", "Bearer " + SupportUtil.WHATSAPP_PUSH_KEY);
File file=null;
try {
file = ResourceUtils.getFile("/home/ubuntu/DanFagin.pdf");
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file",file,ContentType.APPLICATION_OCTET_STREAM,"DanFagin.pdf");
org.apache.http.HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
try (CloseableHttpResponse response = client.execute(httpPost)) {
System.out.println(response.getEntity().toString());
responseMsg= EntityUtils.toString(response.getEntity(),"UTF-8");
System.out.println(responseMsg);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Response is closed");
System.out.println(responseMsg);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Client is closed");
return responseMsg;
}
So here above I've built the entity of HttpResponse (earlier in code), and then trying to use EntityUtils.toString(entity)
method for getting content of the entity. But when I use the method, I get the following error:
org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:263)
at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:222)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:183)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.Reader.read(Reader.java:140)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:227)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:270)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:290)
at com.wingsure.WSWhatsapp.repository.RestTemplateImpl.uploadDocument(RestTemplateImpl.java:564)
at com.wingsure.WSWhatsapp.service.RestTemplateService.uploadDocument(RestTemplateService.java:49)
at com.wingsure.WSWhatsapp.repository.WebHookRepository.getWebHookDetails(WebHookRepository.java:60)
at com.wingsure.WSWhatsapp.service.ProductService.getWebHookDetails(ProductService.java:84)
at com.wingsure.WSWhatsapp.controller.ProductController.getWebHookDetails(ProductController.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
etc
Line 564 is where the EntityUtils.toString()
method is, and without it, I don't receive any error. On the other side, didn't find any other way how can I obtain the content of the entity, ie. the JSON string, without using EntityUtils.toString()
method.
Does someone perhaps know what can be issue here? I would greatly appreciate any help and suggestion.. Thank you in each case.
回答1:
Try not to close the connection
httpclient.close();
response.close();
Let the framework close it for you. I never remember closing this before. Just my suggestion.
来源:https://stackoverflow.com/questions/63220523/extracting-json-from-response-as-responseentityproxycontent-type-application