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. }

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android:versionCode和android:versionNa
  2. Listview
  3. Android开发平台部署
  4. Android常用的技术点
  5. Android:用定时器刷 timer 新界面
  6. android环境部署(1.1)
  7. JS判断Android、iOS或浏览器的多种方法(
  8. Android(安卓)Studio Check for Update
  9. Android(安卓)各种布局技术-五大布局对象
  10. Android开发者实用代码片段