android HttpClient 上传图片

  1. import java.io.File;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.HttpVersion;
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.FileEntity;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.apache.http.params.CoreProtocolPNames;
  10. import org.apache.http.util.EntityUtils;


  11. public class PostFile {
  12.   public static void main(String[] args) throws Exception {
  13.     HttpClient httpclient = new DefaultHttpClient();
  14.     httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

  15.     HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
  16.     File file = new File("c:/TRASH/zaba_1.jpg");

  17.     FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");

  18.     httppost.setEntity(reqEntity);
  19.     reqEntity.setContentType("binary/octet-stream");
  20.     System.out.println("executing request " + httppost.getRequestLine());
  21.     HttpResponse response = httpclient.execute(httppost);
  22.     HttpEntity resEntity = response.getEntity();

  23.     System.out.println(response.getStatusLine());
  24.     if (resEntity != null) {
  25.       System.out.println(EntityUtils.toString(resEntity));
  26.     }
  27.     if (resEntity != null) {
  28.       resEntity.consumeContent();
  29.     }

  30.     httpclient.getConnectionManager().shutdown();
  31.   }
  32. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值