少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          // 上傳一般的參數(shù)
               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);
               // 執(zhí)行postMethod
               int status = client.executeMethod(postMethod);
               System.out.println(status + "--------狀態(tài)------------");
               if (status!= HttpStatus.SC_OK) {
                   System.out.println("--------fail-------");
               } else if (status== HttpStatus.SC_OK) {
                   String str=postMethod.getResponseBodyAsString();
                   System.out.println("---------服務(wù)器返回值---------:"+str);
               }
                  return null;

               }


          //上傳帶附件的參數(shù):

          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:類專門用來上傳文件,其子類  ,F(xiàn)ilePart:用來上傳文件的類     StringPart:普通的文本參數(shù)
                      System.out.println("---" + fp);
                  
                      fp.setContentType(MIME.getMIME(file.getName().substring(file.getName().lastIndexOf(".")+1)));
                      
                      //StringPart:普通的文本參數(shù)
                      StringPart uname=new StringPart("username", "aa");            
                      StringPart pass=new StringPart("password", "123456"); 

                      Part[] parts = {uname,pass,fp}; 
                      
                     //對于MIME類型的請求,httpclient建議全用MulitPartRequestEntity進(jìn)行包裝
                      MultipartRequestEntity mre=new MultipartRequestEntity(parts,filePost.getParams());
                      
                      filePost.setRequestEntity(mre);
                      
                      HttpClient client = new HttpClient();
                      client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 設(shè)置連接時間
                      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()+ "---------服務(wù)器返回值---------");
                      }

                  } 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:類專門用來上傳文件,其子類  ,F(xiàn)ilePart:用來上傳文件的類     StringPart:普通的文本參數(shù)
                      FilePart fp2=new FilePart("file2", file2);
                      System.out.println("---" + fp);
                      fp.setContentType(MIME.getMIME(file.getName().substring(file.getName().lastIndexOf(".")+1)));
                      
                      //StringPart:普通的文本參數(shù)
                      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);// 設(shè)置連接時間
                      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()+ "---------服務(wù)器返回值---------");
                      }

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


          測試:

          public static void main(String[] args) throws HttpException, IOException {
                  // 上傳一般的參數(shù)
                  TestParam();

                  ParamFile();
                  
                  ParamFiles();
              
              }



          posted on 2012-05-28 16:59 abin 閱讀(6338) 評論(0)  編輯  收藏 所屬分類: httpClient
          主站蜘蛛池模板: 长葛市| 冕宁县| 河北区| 应城市| 华宁县| 中山市| 尼勒克县| 宁强县| 台东市| 松江区| 海安县| 青阳县| 东乡县| 信阳市| 太湖县| 绍兴市| 宁城县| 昌吉市| 宁明县| 池州市| 额济纳旗| 鄂温| 光山县| 车险| 璧山县| 崇礼县| 南江县| 南开区| 周口市| 西畴县| 河间市| 新建县| 榕江县| 九台市| 溆浦县| 桂阳县| 温宿县| 尼玛县| 榆中县| 乌鲁木齐县| 府谷县|