hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "MaiZhang (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (HTTPCLIENT-1594) MultipartEntity doesn't work when I set Charset for it
Date Fri, 09 Jan 2015 09:28:35 GMT

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

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

OK,the method is very simple:
public void postUploadFile(AccessoryBean entity) {
    String strReturn = "";
		try {
                        //here,if I use this method,it works well,but,the Chinese file name
looks like "??????"
			MultipartEntity mult = new MultipartEntity();
                        //if I use this method,the server can't get any data
                        //MultipartEntity mult = new MultipartEntity(null,null,Charset.forName("UTF-8"));
			mult.addPart("k", new StringBody("testtoken"));
			mult.addPart("attfile", new FileBody(new File(entity.getFilePath())));
			
			
			
			HttpClient httpclient = new DefaultHttpClient();

			InputStream is = null;
			HttpPost request = new HttpPost("http://xxxxxx");
			HttpParams params = new BasicHttpParams();
			HttpProtocolParams.setUserAgent(httpclient.getParams(), Constants.USER_AGENT);  //设置代理
			request.setParams(params);

			try {
				request.setEntity(mult);
				HttpResponse httpResponse = httpclient.execute(request);
				LogUtil.info(Server.class, " http file upload response state  is:" + httpResponse.getStatusLine().getStatusCode());

				if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					is = httpResponse.getEntity().getContent();
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					byte[] buf = new byte[1024];
					int len = -1;
					while ((len = is.read(buf)) != -1) {
						baos.write(buf, 0, len);
					}
					strReturn  =  new String(baos.toByteArray(), "utf-8");
				}

				LogUtil.info(Server.class, " http file upload result is:" + strReturn);
			} catch (Exception e) {
				e.printStackTrace();
				LogUtil.error(Server.class, " http file upload exception is:" + e.toString());
			}
		} catch (UnsupportedEncodingException e) {
		    LogUtil.error(getClass(), e.getMessage());
		}
}

> 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: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message