少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
// 上传一般的参数
     public static String TestParam() throws HttpException, IOException {
     String url = "http://localhost:8080/IphoneTest/rece.do?method=receive";
     HttpClient client = new HttpClient();
     PostMethod postMethod = new PostMethod(url);
    
     // 填入各个表单域的值
     NameValuePair[] data = { new NameValuePair("username", "username"),
     new NameValuePair("passwd", "123456") };
     // 将表单的值放入postMethod中
     postMethod.setRequestBody(data);
     // 执行postMethod
     int status = client.executeMethod(postMethod);
     System.out.println(status + "--------状态------------");
     if (status!= HttpStatus.SC_OK) {
         System.out.println("--------fail-------");
     } else if (status== HttpStatus.SC_OK) {
         String str=postMethod.getResponseBodyAsString();
         System.out.println("---------服务器返回值---------:"+str);
     }
        return null;

     }


//上传带附件的参数:

public static String ParamFile() {
        String path = "D:\\KuGou\\b.txt";
        File file = new File(path);
        if (!file.exists()) {
            return "文件不存在!";
        }
        String url = "http://localhost:8080/IphoneTest/rece.do?method=receive";
        PostMethod filePost = new PostMethod(url);
        try {
            
            //FilePart:用来上传文件的类
            FilePart fp = new FilePart("file", file); //Part:类专门用来上传文件,其子类  ,FilePart:用来上传文件的类     StringPart:普通的文本参数
            System.out.println("---" + fp);
        
            fp.setContentType(MIME.getMIME(file.getName().substring(file.getName().lastIndexOf(".")+1)));
            
            //StringPart:普通的文本参数
            StringPart uname=new StringPart("username", "aa");            
            StringPart pass=new StringPart("password", "123456"); 

            Part[] parts = {uname,pass,fp}; 
            
           //对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装
            MultipartRequestEntity mre=new MultipartRequestEntity(parts,filePost.getParams());
            
            filePost.setRequestEntity(mre);
            
            HttpClient client = new HttpClient();
            client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 设置连接时间
            int status = client.executeMethod(filePost);
            System.out.println(status + "--------------");
            if (status != HttpStatus.SC_OK) {
                System.out.println(status + "--------------fail----");
            } else if (status == HttpStatus.SC_OK) {
                String str = "";            
                str = filePost.getResponseBodyAsString();
                System.out.println(filePost.getResponseBodyAsString()+ "---------服务器返回值---------");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


//同时上传几个附近

public static String ParamFiles() {
        String path = "D:\\KuGou\\b.txt";
        String path2 = "D:\\KuGou\\a.txt";
        File file = new File(path);
        File file2=new File(path2);
        if (!file.exists()) {
            return "文件不存在!";
        }
        if (!file2.exists()) {
            return "文件不存在!";
        }
        String url = "http://localhost:8080/IphoneTest/rece.do?method=receive";
        PostMethod filePost = new PostMethod(url);
        try {
            
            //FilePart:用来上传文件的类
            FilePart fp = new FilePart("file", file); //Part:类专门用来上传文件,其子类  ,FilePart:用来上传文件的类     StringPart:普通的文本参数
            FilePart fp2=new FilePart("file2", file2);
            System.out.println("---" + fp);
            fp.setContentType(MIME.getMIME(file.getName().substring(file.getName().lastIndexOf(".")+1)));
            
            //StringPart:普通的文本参数
            StringPart uname=new StringPart("username", "aa");            
            StringPart pass=new StringPart("password", "123456"); 
            Part[] parts = {uname,pass,fp,fp2}; 
            
            MultipartRequestEntity mre=new MultipartRequestEntity(parts,filePost.getParams());
            
            filePost.setRequestEntity(mre);
            
            HttpClient client = new HttpClient();
            client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 设置连接时间
            int status = client.executeMethod(filePost);
            System.out.println(status + "--------------");
            if (status != HttpStatus.SC_OK) {
                System.out.println(status + "--------------fail----");
            } else if (status == HttpStatus.SC_OK) {
                String str = "";
                str = filePost.getResponseBodyAsString();
                System.out.println(filePost.getResponseBodyAsString()+ "---------服务器返回值---------");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


测试:

public static void main(String[] args) throws HttpException, IOException {
        // 上传一般的参数
        TestParam();

        ParamFile();
        
        ParamFiles();
    
    }



posted on 2012-05-28 16:59 abin 阅读(6311) 评论(0)  编辑  收藏 所属分类: httpClient

只有注册用户登录后才能发表评论。


网站导航: