[ 
https://issues.apache.org/jira/browse/HTTPCLIENT-1594?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14273188#comment-14273188
 ] 

MaiZhang commented on HTTPCLIENT-1594:
--------------------------------------

Hello my friend,now I can show a test case:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;



public class TestMime {

        public static void sendFile(){
                MultipartEntityBuilder mult = MultipartEntityBuilder.create();
                //I use a wrong token cause it's just a test.If I want to get a 
right token,I must login the server,
                //it's hard to do it here.
                mult.addTextBody("k", "a_test_token");
                mult.addBinaryBody("attfile", new File("C://测试.txt"));
                //this is just a test method,if I set the carset,the server 
will return:{"r":10500,"d":"access_token not found"}
                //if I don't set the charset,the server will 
return:{"r":10504,"d":"verify access_token failed"}
//              mult.setCharset(Charset.forName("UTF-8"));
                
                
                HttpClient httpclient = new DefaultHttpClient();

                InputStream is = null;
                HttpPost request = new 
HttpPost("http://m0.mail.sina.com.cn/api/upload.php";);
                HttpParams params = new BasicHttpParams();
                HttpProtocolParams.setUserAgent(httpclient.getParams(), 
"SMail/1.0.3");  //设置代理
                request.setParams(params);
                String result = "";

                try {
                        request.setEntity(mult.build());
                        HttpResponse httpResponse = httpclient.execute(request);

                        if (httpResponse.getStatusLine().getStatusCode() == 
HttpStatus.SC_OK) {
                                is = httpResponse.getEntity().getContent();
                                result = inStream2String(is);
                        }

                } catch (Exception e) {
                        e.printStackTrace();
                }
                System.out.println(result);
                
        }
        
        private static String inStream2String(InputStream is) throws Exception {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int len = -1;
                while ((len = is.read(buf)) != -1) {
                        baos.write(buf, 0, len);
                }
                return new String(baos.toByteArray(), "utf-8");
        }

        public static void main(String[] args){
                sendFile();
        }
}


these are the jars I used:
commons-codec-1.6.jar
commons-logging-1.1.3.jar
httpclient-4.3.6.jar
httpcore-4.3.3.jar
httpmime-4.3.6.jar

If I don't set the charset,the server will receive the token and check it;but 
if I set the charset the server will not receive the token.

> MultipartEntity doesn't work when I set Charset for it
> ------------------------------------------------------
>
>                 Key: HTTPCLIENT-1594
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1594
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>          Components: HttpMime
>    Affects Versions: 4.2.6
>         Environment: Client info: Android,Sumsung note3
> Server info: php server
>            Reporter: MaiZhang
>              Labels: features
>
> I need upload files to my server,but the file name maybe Chinese.So I want to 
> set charset to MultipartEntity,cuz its default charset is US-ASCII,the file 
> name be sended to server looks like "?????.txt" eg. .But, when I set UTF-8 to 
> it,the server can't get any data.
> MultipartEntity mult = new MultipartEntity();
>                       mult.addPart("k", new 
> StringBody(getAccessToken().getAccessToken()));
>                       mult.addPart("attfile", new FileBody(new 
> File(entity.getFilePath())));
> This  code works well,but::::::
> MultipartEntity mult = new 
> MultipartEntity(null,null,Charset.forName("UTF-8"));
>                       mult.addPart("k", new 
> StringBody(getAccessToken().getAccessToken()));
>                       mult.addPart("attfile", new FileBody(new 
> File(entity.getFilePath())));
> can't post data to server,it's too strange!!!
> AND!!!!!!!
> if I use 
> MultipartEntity mult = new MultipartEntity(); 
> this way and  set breakpoint at the second line,and change the value of 
> multipart-charset-canonicalName to "UTF-8",the server will get the right file 
> name,I think if I set charset to a MultipartEntity object,it will not work.
> And, I tested httpmime4.3.x,by MultipartEntityBuilder,it doesn't work too.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to