Настройка кодировки для Multipart Entity - PullRequest
17 голосов
/ 26 апреля 2011

Я хочу установить кодировку UTF-8 для объекта MultipartEntity или для объекта StringBody. Есть ли способ сделать это? Я знаю, как установить кодировку, но не кодировку.

Спасибо.

Ответы [ 2 ]

37 голосов
/ 04 марта 2012

Это из сообщения anddev.org на эта ссылка , но в данный момент не работает, поэтому я вставил фрагмент ниже.Я не пробовал этот код, но надеюсь, он поможет.

MultipartEntity multipart = new MultipartEntity();
File file = new File("/filepath");  // File with some location (filepath)
Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
FileBody fileB = new FileBody(file); // Create a new FileBody with the above mentioned file
multipart.addPart("data", fileB); // Add the part to my MultipartEntity. "data" is parameter name for the file
StringBody stringB;  // Now lets add some extra information in a StringBody
try {
    stringB = new StringBody("I am the caption of the file",chars);  // Adding the content to the StringBody and setting up the encoding
    multipart.addPart("caption", stringB); // Add the part to my MultipartEntity
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

HttpPost post = new HttpPost(url); // Setting up a HTTP Post method with the target url
post.setEntity(multipart); // Setting the multipart Entity to the post method
HttpResponse resp = client.execute(post);  // Using some HttpClient (I'm using DefaultHttpClient) to execute the post method and receive the response
0 голосов
/ 22 августа 2014

Метод, описанный выше, не рекомендуется.

Есть ответ, который сделан правильно сейчас. MultipartEntityBuilder и Charset

...