代码示例: 以下为使用 Apache HttpClient 实现上传的示例代码:
public void uploadBmpFile() {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://example.com/upload");
MultipartEntity entity = new MultipartEntity();
try {
File file = new File("/sdcard/image.bmp");
FileBody fileBody = new FileBody(file);
entity.addPart("file", fileBody);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(httpEntity);
Log.d("Upload Result", result);
} catch (IOException e) {
e.printStackTrace();
}
}