Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342710
  • 博文数量: 88
  • 博客积分: 1673
  • 博客等级: 上尉
  • 技术积分: 934
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-20 13:51
文章分类

全部博文(88)

文章存档

2016年(1)

2015年(4)

2014年(3)

2013年(7)

2012年(11)

2011年(1)

2009年(61)

我的朋友

分类: Java

2014-03-03 11:45:07


点击(此处)折叠或打开

  1. package test.httpclient4;
  2.       
  3.     import java.io.File;
  4.     import java.io.IOException;
  5.       
  6.     import org.apache.http.HttpEntity;
  7.     import org.apache.http.HttpResponse;
  8.     import org.apache.http.HttpStatus;
  9.     import org.apache.http.client.ClientProtocolException;
  10.     import org.apache.http.client.HttpClient;
  11.     import org.apache.http.client.methods.HttpPost;
  12.     import org.apache.http.entity.mime.MultipartEntity;
  13.     import org.apache.http.entity.mime.content.FileBody;
  14.     import org.apache.http.entity.mime.content.StringBody;
  15.     import org.apache.http.impl.client.DefaultHttpClient;
  16.     import org.apache.http.util.EntityUtils;
  17.       
  18.     public class SendFile {
  19.       
  20.         public static void main(String[] args) throws ClientProtocolException,
  21.                 IOException {
  22.             HttpClient httpclient = new DefaultHttpClient();
  23.             //请求处理页面
  24.             HttpPost httppost = new HttpPost(
  25.                     "");
  26.             //创建待处理的文件
  27.             FileBody file = new FileBody(new File("d:/22.rar"));
  28.             //创建待处理的表单域内容文本
  29.             StringBody descript = new StringBody("0431.la");
  30.       
  31.             //对请求的表单域进行填充
  32.             MultipartEntity reqEntity = new MultipartEntity();
  33.             reqEntity.addPart("file", file);
  34.             reqEntity.addPart("descript", descript);
  35.             //设置请求
  36.             httppost.setEntity(reqEntity);
  37.             //执行
  38.             HttpResponse response = httpclient.execute(httppost);
  39.             //HttpEntity resEntity = response.getEntity();
  40.             //System.out.println(response.getStatusLine());
  41.             if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
  42.                 HttpEntity entity = response.getEntity();
  43.                 //显示内容
  44.                 if (entity != null) {
  45.                     System.out.println(EntityUtils.toString(entity));
  46.                 }
  47.                 if (entity != null) {
  48.                     entity.consumeContent();
  49.                 }
  50.             }
  51.         }
  52.     }
这里说明一下 需要一个额外的包,apache 的mime4j 的lib。
阅读(1010) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~