I am having trouble sending an HttpEntity created using the 
MultipartEntityBuilder on the HttpAsyncClient. 

Based on this exchange from the httpclient-users mailing list: 
http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/201206.mbox/%3c1339240012.2405.12.camel@ubuntu%3e
 
I thought the solution was to wrap the HttpEntity in a BufferedHttpEntity, but 
that doesn't help. 

The javadoc for the BufferedHttpEntity says "If the wrapped entity is 
repeatable itself, calls are passed through. If the wrapped entity is not 
repeatable, the content is read into a buffer once and provided from there as 
often as required.". However, even if I create the HttpEntity (using 
MultipartEntityBuilder) in such a way that it reports itself to not be 
repeatable (isRepeatable() returns false), calls to getContent() throw an 
UnsupportedOperationException. The stack trace shows that it is still passing 
the call to getContent through to the MultipartFormEntity. 

Here is the code that I am using to test this: 

HttpEntity mpEntity = MultipartEntityBuilder.create() 
.addPart("image", new InputStreamBody(imageInputStream, 
ContentType.create("image/jpeg"))) 
.build(); 

System.out.println("The mpEntity isRepeatable = " + mpEntity.isRepeatable()); 
BufferedHttpEntity bufferedMpEntity = new BufferedHttpEntity(mpEntity); 
bufferedMpEntity.getContent(); 

Which gives the following output: 

The mpEntity isRepeatable = false 
Exception in thread "main" java.lang.UnsupportedOperationException: Multipart 
form entity does not implement #getContent() 
at 
org.apache.http.entity.mime.MultipartFormEntity.getContent(MultipartFormEntity.java:92)
 
at org.apache.http.util.EntityUtils.toByteArray(EntityUtils.java:122) 
at org.apache.http.entity.BufferedHttpEntity.<init>(BufferedHttpEntity.java:63) 
at Test.main(Test.java:440) 

I actually want to create the HttpEntity this way: 

HttpEntity mpEntity = MultipartEntityBuilder.create() 
.addPart("image", new ByteArrayBody(imageAsBytes, 
ContentType.create("image/jpeg"), "itemImage.jpg")) 
.build(); 

This gives the same exception as above. Since in this case 
mpEnity.isRepeatable() returns true, I had thought this was the cause of the 
exception. However that seems not to be the case. 

Is there something else that I need to do in order to send a multipart entity 
using the HttpAsyncClient? 

Thanks, 

Hal 





Reply via email to