我想在設置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 –