通过MultipartEntity发送Unicode字符 [英] Send Unicode characters via MultipartEntity

查看:104
本文介绍了通过MultipartEntity发送Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种使用MultipartEntity内容类型将图像和文本作为HttpPost发送的方法。一切都适用于英文符号,但对于unicode符号(例如Cyrliics),它只发送???。所以,我想知道,如何正确设置MultipartEntity的UTF-8编码,因为我已经尝试了几个苏打,建议在SO上,但没有一个工作。
这里我已经拥有:

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.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
mpEntity.setCharset(Consts.UTF_8);

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 = mpEntity.build();
httpPost.setEntity(fileBody);  

HttpResponse httpResponse = httpclient.execute(httpPost);

UPD
我尝试按照@Donaudampfschifffreizeitfahrt使用InputStream建议。现在我得到了 字符。

UPD 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);

还试过:

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


推荐答案

对于那些坚持这个问题的人来说,这是我如何解决它:

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

我调查了apache http组件库的源代码,发现如下:

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

org.apache.http.entity.mime.HttpMultipart::doWriteTo()


case BROWSER_COMPATIBLE:
    // 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);
    }
    break;

所以,好像它是apache lib中的某种bug /功能,只允许添加如果此部分的文件名不为null,则将内容类型标头添加到MultipartEntity的一部分。所以我修改了我的代码:

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);

并且字符串部分出现Content-type标头,现在符号被正确编码和解码。

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

这篇关于通过MultipartEntity发送Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆