0

I m trying to create a chunked transfer encoding.

String url ="https://hostname/path/";
File testUploadFile = new File("filePath");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpEntity postData = MultipartEntityBuilder.create().addBinaryBody("uploadFile", testUploadFile).build();
HttpUriRequest postRequest = RequestBuilder.put(url).setEntity(postData)
        .addHeader("Content-Type","application/json")
        .addHeader("User-Agent","java-prg")
        .build();

HttpResponse response =  httpClient.execute(postRequest);
BufferedReader bufferedReader  = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuffer stringBuffer= new StringBuffer();
String line ="";
System.out.println("Result "+response.getStatusLine().getStatusCode());
while((line=bufferedReader.readLine()) != null) {
    stringBuffer.append(line);
}

Even if I add

.addHeader("Transfer-Encoding","chunked")
org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187)
Caused by: org.apache.http.ProtocolException: Transfer-encoding header already present
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
TomJava
  • 499
  • 1
  • 8
  • 24
  • How do you conclude it is not chunking? Also, why do you want it to chunk? Likely it doesn't use chunking in this case because it knows the total size ahead of time, because files have a known size. Also, which version of the Apache HTTP client are you using (right now I've assumed the 4.x version). – Mark Rotteveel Mar 21 '23 at 11:28

0 Answers0