Can multicomponent and multichannel HTTP coexist?

I am using apache HttpClient to send multiple files to the server. Here is the code:

  public static HttpResponse stringResponsePost(String urlString, String content, byte[] image, HttpContext localContext, HttpClient httpclient) throws Exception { URL url = new URL(URLDecoder.decode(urlString, "utf-8")); URI u = url.toURI(); HttpPost post = new HttpPost(); post.setURI(u); MultipartEntity reqEntity = new MultipartEntity(); StringBody sb = new StringBody(content, HTTP_CONTENT_TYPE_JSON, Charset.forName("UTF-8")); ByteArrayBody ib = new ByteArrayBody(image, HTTP_CONTENT_TYPE_JPEG, "image"); reqEntity.addPart("interview_data", sb); reqEntity.addPart("interview_image", ib); post.setEntity(reqEntity); HttpResponse response = null; response = httpclient.execute(post, localContext); return response; } 

The problem is that the MultipartEntity class has only the isChunked() method (which always returns false), there is no "setChunked (boolean)" option if I want to enable pushable encoding for my multi-part entity.

My question is:

  • Can HTTP multipart and chunking coexist according to protocol specification? If not, then why do other objects, such as the InputStreamEntity class, have setChunked(boolean) , where MultipartEntity does not work?

  • Is there a way to publish multiple files β€œat once” with chunking enabled, more preferably with apache libraries?

+7
java multipartentity chunked-encoding
source share
1 answer

Got a solution for my second question, the trick is to write MultipartEntity in a ByteArrayOutputStream , create a ByteArrayEntity from ByteArrayOutputStream and include chunking in it. Here is the code:

  ByteArrayOutputStream bArrOS = new ByteArrayOutputStream(); // reqEntity is the MultipartEntity instance reqEntity.writeTo(bArrOS); bArrOS.flush(); ByteArrayEntity bArrEntity = new ByteArrayEntity(bArrOS.toByteArray()); bArrOS.close(); bArrEntity.setChunked(true); bArrEntity.setContentEncoding(reqEntity.getContentEncoding()); bArrEntity.setContentType(reqEntity.getContentType()); // Set ByteArrayEntity to HttpPost post.setEntity(bArrEntity); 
+8
source

All Articles