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

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 连平县| 秦皇岛市| 奇台县| 彭州市| 焦作市| 天气| 揭西县| 绍兴市| 滁州市| 罗山县| 渝中区| 安新县| 辽源市| 建湖县| 修水县| 京山县| 西峡县| 姜堰市| 鞍山市| 容城县| 芒康县| 巨野县| 临沂市| 龙井市| 宁晋县| 高陵县| 易门县| 蓬溪县| 齐齐哈尔市| 乾安县| 黔西县| 昆山市| 东兴市| 绥江县| 车险| 莱芜市| 仁化县| 英山县| 英德市| 陆丰市| 邓州市|