<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 閱讀(6335) 評論(0)  編輯  收藏 所屬分類: httpClient
    主站蜘蛛池模板: 18pao国产成视频永久免费| 日产乱码一卡二卡三免费| 国产成人精品日本亚洲直接| 成人毛片18岁女人毛片免费看| 久久精品免费一区二区| 亚洲噜噜噜噜噜影院在线播放| 中文字幕久精品免费视频| 久久久国产精品亚洲一区| 国产精品麻豆免费版| 99精品免费观看| 在线观看亚洲网站| 精品亚洲成AV人在线观看| 免费国产在线观看| 很黄很黄的网站免费的| free哆拍拍免费永久视频| 激情综合亚洲色婷婷五月APP| xx视频在线永久免费观看| 思思久久99热免费精品6| 亚洲精品视频在线观看免费| 免费成人黄色大片| 啦啦啦高清视频在线观看免费| 亚洲国产人成网站在线电影动漫| 国产性生大片免费观看性| 亚洲伊人久久大香线蕉影院| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 亚洲色大18成人网站WWW在线播放| 国产91色综合久久免费| 国产精品内射视频免费| 亚洲女女女同性video| 亚洲一区免费观看| 亚洲精品无码AV人在线播放| 国产免费人成视频在线观看| 久久午夜免费视频| 最近中文字幕mv免费高清在线| 亚洲综合图片小说区热久久| 亚洲日韩乱码中文无码蜜桃臀网站 | 亚洲视频人成在线播放| 大学生一级特黄的免费大片视频| 亚洲精品无码av片| 亚洲精品美女久久久久| 亚洲AV无码久久寂寞少妇|