People who have used HttpClient know that you can add text information to upload by Addtextbody method, but if you want to upload Chinese, or if you have a Chinese name file, the problem is garbled, the solution is simple:
The first step: Set the Multipartentitybuilder encoding method to UTF-8.
Builder.setcharset (Charset.forname (HTTP. Utf_8)); //
Step Two: Create the ContentType object and specify the UTF-8 encoding.
Step three: Use addpart+ stringbody instead of Addtextbody. Such as:
Stringbody stringbody=New stringbody (" Chinese garbled ", ContentType); Builder.addpart ("test", Stringbody);
Enclose the complete code:
ContentType ContentType =contenttype.create (HTTP. Plain_text_type, HTTP. UTF_8); HttpClient Client=NewDefaulthttpclient ();//turn on a client HTTP requestHttpPost post =NewHttpPost (URL);//Create an HTTP POST requestMultipartentitybuilder Builder =multipartentitybuilder.create (); Builder.setcharset (Charset.forname (HTTP. Utf_8));//set the encoding format of the requestBuilder.setmode (httpmultipartmode.browser_compatible);//set browser compatibility modeintCount=0; for(File file:files) {//filebody filebody = new Filebody (file);//convert files to stream objects Filebody//builder.addpart ("File" +count, filebody);Builder.addbinarybody ("file"+count, file); Count++;} Builder.addtextbody ("Method",params.Get("Method"));//Set Request ParametersBuilder.addtextbody ("FileTypes",params.Get("FileTypes"));//Set Request ParametersStringbody stringbody=NewStringbody ("Chinese garbled", ContentType); Builder.addpart ("Test", Stringbody); Httpentity Entity= Builder.build ();//generate an HTTP POST entityPost.setentity (entity);//Set Request ParametersHttpResponse response = Client.execute (POST);//initiating the request and returning the requested responseif(Response.getstatusline (). Getstatuscode () = = $) { return true;}return false;
Android about HttpClient upload Chinese garbled solution