<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
    主站蜘蛛池模板: 免费国产va在线观看| 无码欧精品亚洲日韩一区| 久久精品国产亚洲av麻豆蜜芽 | 免费A级毛片无码视频| 丁香五月亚洲综合深深爱| 窝窝影视午夜看片免费| 免费大黄网站在线观看| 香蕉视频在线观看免费| 亚洲日本一区二区一本一道| 国产亚洲精彩视频| 亚洲精品人成无码中文毛片| a级毛片免费观看网站| 久久99亚洲综合精品首页| 亚洲免费观看视频| 亚洲精品在线播放| 无人在线观看免费高清视频 | a级片在线免费看| 久久久久亚洲AV无码永不| 天天影院成人免费观看| 亚洲国产精品自在自线观看| 国产又大又长又粗又硬的免费视频| 精品国产亚洲一区二区三区在线观看 | 国产精品久久香蕉免费播放| 免费国产污网站在线观看不要卡| 亚洲一区二区精品视频| 日本高清高色视频免费| 亚洲AV无码久久久久网站蜜桃| 永久免费bbbbbb视频| 一边摸一边桶一边脱免费视频| 亚洲妇熟XXXX妇色黄| 在线观看免费高清视频| 美女免费精品高清毛片在线视| 最新国产AV无码专区亚洲| 最近免费中文字幕大全高清大全1 最近免费中文字幕mv在线电影 | 亚洲一区二区女搞男| 免费h片在线观看网址最新| 亚洲精品无码av片| 亚洲色爱图小说专区| 无限动漫网在线观看免费| 国产无遮挡色视频免费观看性色| 亚洲精品午夜视频|