<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      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 閱讀(6342) 評論(0)  編輯  收藏 所屬分類: httpClient
    主站蜘蛛池模板: 99爱在线观看免费完整版| 亚洲免费一级视频| 人妻巨大乳hd免费看| 国产免费观看网站| 国产国拍亚洲精品福利 | 免费一级国产生活片| 99久久人妻精品免费一区| 国产亚洲一区二区在线观看| 国产精品成人啪精品视频免费| 亚洲国产午夜福利在线播放| 人妻巨大乳hd免费看| 亚洲日产无码中文字幕| 无码av免费一区二区三区| 中文字幕亚洲第一在线| 青娱乐免费在线视频| 精品国产人成亚洲区| 亚洲不卡1卡2卡三卡2021麻豆| 激情吃奶吻胸免费视频xxxx| 免费人成视频x8x8入口| 美女巨胸喷奶水视频www免费| 亚洲国产精品一区二区第一页| 亚洲一区欧洲一区| 国产日产成人免费视频在线观看| 日韩在线观看视频免费| 亚洲成AV人片在线观看ww| 中国人xxxxx69免费视频| 亚洲色成人四虎在线观看| 亚洲高清无码专区视频| 国产精品免费AV片在线观看| 久久精品国产亚洲αv忘忧草| 国产真实伦在线视频免费观看| 成年大片免费高清在线看黄| 亚洲AV成人精品网站在线播放| 国产免费av片在线看| 黄网站色成年片大免费高清| 亚洲av午夜福利精品一区| 免费AA片少妇人AA片直播| 日韩毛片免费一二三| 久久亚洲春色中文字幕久久久| 欧洲美熟女乱又伦免费视频| 亚洲永久在线观看|