Setting encoding for a Multipart Entity
Asked Answered
R

2

17

I want to set a UTF-8 Encoding to a MultipartEntity object or to StringBody object. Is there any way to do it? I know how to set the Charset but not the Encoding.

Thank you.

Repression answered 26/4, 2011 at 14:3 Comment(1)
I have also same problem... Please suggest the solution I have used the following code to do so....MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE, "whatever", CharsetUtil.getCharset("UTF-8"));Notify
S
38

This is from an anddev.org post at this link, but is currently down, so I have pasted the snippet below. I haven't tried this code, but I hope it helps.

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
Sapper answered 4/3, 2012 at 4:51 Comment(3)
Where to add chars which u defined above as Charset chars = Charset.forName("UTF-8"); ??Tondatone
See stringB = new StringBody("I am the caption of the file",chars); Sapper
unfortunately - deprecated :-(Bleeding
B
0

Method, which is above is deprecated.

There is answer which is made in correct way now. MultipartEntityBuilder and Charset

Bleeding answered 22/8, 2014 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.