我想在设置Content-Type后使用MultipartEntityBuilder将文件从Android上传到服务器,并且无法上传。如何使用MultipartEntityBuilder在Android上载文件?
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
Log.d("Name", firstname+" "+lastname);
builder.addTextBody("firstName", firstname);
builder.addTextBody("lastName", lastname);
Log.d("ProfilePicturePath", picture);
if (picture.length()>0) {
builder.addPart("picture", new FileBody(new File(picture)));
}
Log.d("Boundary", boundary);
Log.d("URL", url);
String credentials = Base64.encodeToString((username+":"+password).getBytes(), Base64.NO_WRAP);
httpPost.setHeader("Authorization", "Basic "+credentials);
httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
Log.d("BasicAuthorization", credentials);
httpPost.setEntity(builder.build());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
请通过我的代码,并建议我一些解决方案。
任何错误获取内容类型?究竟出了什么问题?你的服务器在PHP?如果是的话发布php脚本。 – greenapps
'Log.d(“ProfilePicturePath”,图片);'。请告诉它什么日志。 – greenapps
ProfilePicturePath - /storage/sdcard0/abcd/IMG_20150601_152933_.JPG –