Java MultipartEntityBuilder 和字符集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19292169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:45:19  来源:igfitidea点击:

MultipartEntityBuilder and Charset

javacharacter-encoding

提问by Amir.F

I upgraded my httpmime package, and now my strings are not sent or received as UTF-8

我升级了我的 httpmime 包,现在我的字符串没有以 UTF-8 格式发送或接收

MultipartEntityBuilder entity = MultipartEntityBuilder.create();
Charset chars = Charset.forName("UTF-8");
entity.setCharset(chars);
entity.addTextBody("some_text", some_text);

HttpPost httppost = new HttpPost(url); 
httppost.setEntity(entity.build());
...and so on..

what am I missing? I used to build a StringBody and set the charset in the stringbody, but that is deprecated now, and it just doesn't seem to work

我错过了什么?我曾经构建一个 StringBody 并在 stringbody 中设置字符集,但现在已弃用,它似乎不起作用

采纳答案by Amir.F

Solved it :) it turns out that ContentType is now important, and I was sending text that was plain, and also some text that was JSON,

解决了 :) 事实证明 ContentType 现在很重要,我发送的是纯文本,还有一些 JSON 文本,

for the plain text, you can use:

对于纯文本,您可以使用:

entity.addTextBody("plain_text",plain_text,ContentType.TEXT_PLAIN);

and for JSON:

对于 JSON:

entity.addTextBody("json_text",json_text,ContentType.APPLICATION_JSON);

that way the charset also works on JSON strings (weird, but now OK)

这样字符集也适用于 JSON 字符串(很奇怪,但现在可以了)

回答by worawee.s

entity.addTextBody("plain_text", plain_text);

will use the default ContentType.TEXT_PLAIN which look like this...

将使用默认的 ContentType.TEXT_PLAIN 看起来像这样......

public static final ContentType TEXT_PLAIN = ContentType.create("text/plain", Consts.ISO_8859_1);

while ContentType.APPLICATION_JSON is using UTF-8 as below

而 ContentType.APPLICATION_JSON 使用 UTF-8 如下

public static final ContentType APPLICATION_JSON = ContentType.create("application/json", Consts.UTF_8);

So what i did was create our own ContentType like this..

所以我所做的是像这样创建我们自己的 ContentType ..

entity.addTextBody("plain_text", plain_text, ContentType.create("text/plain", MIME.UTF8_CHARSET));

回答by Nevin Chen

In my case, I setup the contentType first like this

就我而言,我首先像这样设置 contentType

ContentType contentType = ContentType.create(
                        HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);

and when adding pairs , specify the content type like this

添加对时,请像这样指定内容类型

entityBuilder.addTextBody("title",pic.getTitle(),contentType);

as answered here MultipartEntityBuilder and setCharset for UTF-8 sends empty content

正如这里所回答的MultipartEntityBuilder 和 setCharset for UTF-8 发送空内容

回答by nAkhmedov

Please try this for

请试试这个

Utf-8:

UTF-8:

entity.addTextBody("your text", stringBuffer.toString(), ContentType.create("text/plain", Charset.forName("UTF-8")));

回答by Nom1fan

For those who say the accepted solution did not work for them (it didn't for me either), I solved it a different way, using:

对于那些说接受的解决方案对他们不起作用(对我也不起作用)的人,我以不同的方式解决了它,使用:

builder.addTextBody(key, ????, ContentType.TEXT_PLAIN.withCharset("UTF-8"));