posts - 93,  comments - 2,  trackbacks - 0
          1.建立連接   
               HttpClient httpclient = new DefaultHttpClient();
          2.創(chuàng)建存放路徑
             Environment.getExternalStorageDirectory();
              String saveDir =sdCardDir.getCanonicalPath() +"/DownFile";
              
              File file1 = new File(saveDir);
              if(!file1.exists()){
               file1.mkdir();
              }
          3.選擇傳值方法(post或者get)
              HttpPost httpRequest = new HttpPost(url);
              HttpResponse response = httpclient.execute(httpRequest);
              HttpEntity entity = response.getEntity();
              BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
              InputStream is = bufHttpEntity.getContent();
              FileOutputStream out = new FileOutputStream(saveDir,false);
              bufferedOutputStream = new BufferedOutputStream(
                out);
              bufferedInputStream = new BufferedInputStream(
                is);
              byte[] buf = new byte[4096];
              
              int bytesRead = 0;
              while (bytesRead >= 0) {
               long now = System.currentTimeMillis();
               bufferedOutputStream.write(buf, 0, bytesRead);
               bytesRead = bufferedInputStream.read(buf);
              }
              bufferedOutputStream.flush();

          4.關(guān)閉流及httpclient
              bufferedOutputStream.close();
               out.close();
               if(bufferedOutputStream!=null){
                bufferedOutputStream.close();
               }
               if(out!=null){
                out.close();
               }
               if(bufferedInputStream!=null){
                bufferedInputStream.close();
               }
               if(is!=null){
                is.close();
               }
               shutdownHttpClient();

              private void shutdownHttpClient() {
                  if (httpclient != null && httpclient.getConnectionManager() != null) {
                   httpclient.getConnectionManager().shutdown();
                  }
           }
              
          posted on 2013-05-08 20:30 Terry Zou 閱讀(697) 評論(0)  編輯  收藏 所屬分類: Android
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 丰顺县| 漾濞| 巨鹿县| 龙山县| 永平县| 咸宁市| 阿城市| 得荣县| 林甸县| 通山县| 荥阳市| 菏泽市| 江西省| 通渭县| 甘孜县| 西畴县| 嵩明县| 延长县| 鹤庆县| 民权县| 迁安市| 黔西| 德保县| 睢宁县| 博客| 灌阳县| 新河县| 武城县| 河间市| 平阴县| 石柱| 阿城市| 那坡县| 波密县| 汾阳市| 天峨县| 咸宁市| 广汉市| 台江县| 攀枝花市| 石家庄市|