一般来说,目前在手机端上传图片,方法挺多的,不过常用的就是一个文件流,另一个是二进制转base64的字符流,下面分享一下方法
网络上都是千篇一律的写法,对于上传过程中失败的问题也很少提及,上传失败的bug,主要的原因当然是编码字符的问题了,一般网上都没有介绍,记得要将base64的字符串 转化一下为utf-8,要不然会导致上传失败
网络上都是千篇一律的写法,对于上传过程中失败的问题也很少提及,上传失败的bug,主要的原因当然是编码字符的问题了,一般网上都没有介绍,记得要将base64的字符串 转化一下为utf-8,要不然会导致上传失败
-
- public
static String httpPostBase64(byte[] data, String url,String uid,String blog) { -
String str=null; -
try { -
-
HttpPost httpRequest = new HttpPost(url); -
-
-
-
-
-
List params = new LinkedList(); -
params.add(new BasicNameValuePair("uid", uid)); -
params.add(new BasicNameValuePair("blog", blog)); -
if(data!=null){ -
String data1 = Base64.encodeToString(data, 0, data.length,Base64.DEFAULT); -
params.add(new BasicNameValuePair("img",URLEncoder.encode(data1,"UTF-8"))); -
} -
-
-
-
-
-
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); -
-
-
-
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); -
-
-
-
if (httpResponse.getStatusLine().getStatusCode() != 200) { -
-
Log.d("Code", httpResponse.getStatusLine().toString()); -
-
return str; -
-
}else{ -
HttpEntity httpEntity=httpResponse.getEntity(); -
str = EntityUtils.toString(httpEntity,"UTF-8"); -
System.out.println("-------------------"+str); -
} -
-
} catch (Exception e) { -
-
for (StackTraceElement s : e.getStackTrace()) -
-
Log.d("Exception", s.toString()); -
-
Log.d("Exception", e.getLocalizedMessage()); -
-
return str; -
-
} -
-
return str; -
-
}