mol mol - 1 year ago 187
Apache Configuration Question

Send Unicode characters via MultipartEntity

I have a method to send image and text as a HttpPost using MultipartEntity content type. Everything works great with English symbols, but for unicode symbols (for example Cyrliics) it sends only ???. So, I'm wondering, how to set UTF-8 encoding for MultipartEntity correctly, since I've tried several sulutions, suggested on SO, but none of them worked.
Here what I have already:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();

mpEntity.addPart("image", new FileBody(new File(attachmentUri), ContentType.APPLICATION_OCTET_STREAM));

ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
StringBody stringBody = new StringBody(mMessage, contentType);
mpEntity.addPart("message", stringBody);

final HttpEntity fileBody =;

HttpResponse httpResponse = httpclient.execute(httpPost);

I tried to use InputStream as per @Donaudampfschifffreizeitfahrt suggestion. Now I'm getting ��� characters.

InputStream stream = new ByteArrayInputStream(mMessage.getBytes(Charset.forName("UTF-8")));
mpEntity.addBinaryBody("message", stream);

Also tried:

mpEntity.addBinaryBody("message", mMessage.getBytes(Charset.forName("UTF-8")));

mol mol
Answer Source

To the ones who stuck with this issue, this is how I resolved it:

I investigated apache http components libraries source code and found following:


    // Only write Content-Disposition
    // Use content charset

    final MinimalField cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION);
    writeField(cd, this.charset, out);
    final String filename = part.getBody().getFilename();
    if (filename != null) {
        final MinimalField ct = part.getHeader().getField(MIME.CONTENT_TYPE);
        writeField(ct, this.charset, out);

So, seems like it is some kind of bug / feature in apache lib, which only allowes to add Content-type header to one part of MultipartEntity, if this part has filename not null. So I modified my code as:

Charset utf8 = Charset.forName("utf-8");
ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), utf8);
ContentBody body = new ByteArrayBody(mMessage.getBytes(), contentType, "filename");
mpEntity.addPart("message", body);

And Content-type header appeared for string part, and symbols are now encoded and decoded correctly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download