我希望它可以幫助某人時,你可以提到的文件類型,如FileBody「圖像/ JPEG」,如下代碼
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"url");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("name", new StringBody(name));
reqEntity.addPart("password", new StringBody(pass));
File file=new File("/mnt/sdcard/4.jpg");
ContentBody cbFile = new FileBody(file, "image/jpeg");
reqEntity.addPart("file", cbFile);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
Log.e("Response for POst", s.toString());
需要添加項目中的jar文件的HttpClient-4.2.2.jar,httpmime-4.2.2.jar 。