Oleg,
Thanks for the advice. I chose option 1 and it works fine. It may not be the
most efficient, but it is sufficient for my purposes.
Just in case anyone has the same question in the future, here is a code snippet
that shows how I got this to work:
String boundary = UUID.randomUUID().toString();
HttpEntity mpEntity = MultipartEntityBuilder.create()
.setBoundary("-------------" + boundary)
.addBinaryBody("image1", imageAsBytes, ContentType.create("image/jpeg"),
"image.jpg")
.build();
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
mpEntity.writeTo(baoStream);
HttpEntity nByteEntity = new NByteArrayEntity (baoStream.toByteArray(),
ContentType.MULTIPART_FORM_DATA);
HttpPost httpPost = new HttpPost(urlToPost);
httpPost.setHeader("Content-Type", "multipart/form-data;boundary=-------------"
+ boundary);
httpPost.setEntity(entity);
_httpAsyncClient.execute(httpPost, httpContext, futureCallback);
Thanks,
Hal
----- Original Message -----
From: "Oleg Kalnichevski" <[email protected]>
To: "HttpClient User Discussion" <[email protected]>
Sent: Friday, November 8, 2013 1:38:46 PM
Subject: Re: HttpAsyncClient with MultipartEnityBuilder
On Fri, 2013-11-08 at 09:45 -0800, Harold Rosenberg wrote:
> Actually, one minor correction. In the second case, the exception stack is
> actually slightly different. It is:
>
> The mpEntity isRepeatable = true
> 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.entity.HttpEntityWrapper.getContent(HttpEntityWrapper.java:84)
>
> at
> org.apache.http.entity.BufferedHttpEntity.getContent(BufferedHttpEntity.java:83)
>
> at Test.main(Test.java:440)
>
> So, in this case the exception happens because the call is being passed
> through. In the stack trace from the other case, the exception seems to
> happen when the BufferedHttpEntity tries to buffer the data. Does this mean
> that there is no way to send a Multipart entity using the HttpAsyncClient?
>
Harold
That's the problem with trying to mix an asynchronous transport with an
inherently blocking content producer. MultipartFormEntity can only
produce its content using #writeTo method and cannot be used
asynchronously. You basically have two options. (1) Write out entity
content to ByteArrayOutputStream (or similar) and then create
NByteArrayEntity using the resultant byte array. (2) Create multipart
entity implementation that can produce its content asynchronously.
Hope this helps
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]