少年阿賓

          那些青春的歲月

            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 閱讀(6335) 評論(0)  編輯  收藏 所屬分類: httpClient
          主站蜘蛛池模板: 广昌县| 涪陵区| 西宁市| 松阳县| 东兰县| 永安市| 保康县| 成安县| 诸暨市| 临高县| 新巴尔虎左旗| 吉木萨尔县| 嘉荫县| 芦溪县| 夏津县| 成武县| 兖州市| 富顺县| 孝昌县| 吉安县| 凌海市| 新河县| 武安市| 祁连县| 剑阁县| 定远县| 平遥县| 昌江| 桂阳县| 溧水县| 井研县| 含山县| 台北市| 威海市| 曲靖市| 朝阳市| 卢氏县| 大邑县| 崇左市| 洛扎县| 乌恰县|