posts - 93,  comments - 2,  trackbacks - 0
          1.建立連接   
               HttpClient httpclient = new DefaultHttpClient();
          2.創建存放路徑
             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.關閉流及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 閱讀(698) 評論(0)  編輯  收藏 所屬分類: Android
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 江城| 临江市| 武强县| 扶绥县| 文昌市| 太保市| 盐津县| 嘉定区| 乐至县| 息烽县| 巴楚县| 克拉玛依市| 子长县| 贡山| 阜康市| 墨玉县| 河津市| 图们市| 渝北区| 全州县| 新疆| 黄骅市| 岗巴县| 克拉玛依市| 万山特区| 安福县| 鄂托克旗| 呈贡县| 龙井市| 岳阳市| 上栗县| 叙永县| 车致| 沁阳市| 芷江| 林州市| 大冶市| 和田县| 丹凤县| 阿图什市| 清苑县|