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 閱讀(699) 評論(0)  編輯  收藏 所屬分類: Android
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 临沭县| 三台县| 辽阳市| 海晏县| 汽车| 德钦县| 南平市| 寿阳县| 墨竹工卡县| 上犹县| 宕昌县| 弥勒县| 威海市| 延安市| 汾阳市| 东乡县| 兴国县| 公安县| 三亚市| 湘西| 彰武县| 芷江| 凭祥市| 石阡县| 松滋市| 徐水县| 本溪市| 台南市| 泾阳县| 上林县| 蕲春县| 邯郸县| 鄂托克旗| 南安市| 江华| 崇文区| 梓潼县| 蒙自县| 凤翔县| 临夏县| 武威市|