设置多部分实体的编码

我想设置一个UTF-8编码到一个MultipartEntity对象或StringBody对象。 有没有办法做到这一点? 我知道如何设置字符集,但不是编码。

谢谢。

Solutions Collecting From Web of "设置多部分实体的编码"

这是从这个链接的一个anddev.orgpost,但目前正在下降,所以我已经粘贴下面的代码片段。 我还没有尝试过这个代码,但我希望它有帮助。

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 

上面的方法已被弃用。

现在有正确的方法做的回答。 MultipartEntityBuilder和Charset