標籤:android blog http 使用 strong 檔案
最近做一個項目需要從伺服器下載圖片到本地sdcard,上網尋找了一些例子,下面這個比較合適,原文內容如下:
我們在開發中經常需要從伺服器下載檔案,下載的內容可能有交換的資訊,緩衝的圖片,程式更新包等。我們使用URLConnection來實現下載。先看幾行代碼: String urlDownload = "";urlDownload = "http://www.baidu.com/img/baidu_sylogo1.gif";URL url = new URL(urlDownload ); // 開啟串連 URLConnection con = url.openConnection();// 輸入資料流 InputStream is = con.getInputStream();
如上面的代碼所示,指定一個下載的目標連結,我們上面指定了一個圖片地址。然後構建一個URL對象,調用該 對象的openConnection方法來建立一個資料通路(串連)。代碼的最後一行使用 con.getInputStream,拿到一個輸入資料流對象,通過這個流對象我們就可以讀取到這個檔案的內容了。下面要做的,就是讀取這個流,將流寫入我 們的本地檔案。不過在這之前,我們還要說下這個方法: <span xmlns="http://www.w3.org/1999/xhtml">//獲得檔案的長度int contentLength = con.getContentLength();System.out.println("長度 :"+contentLength)</span>
獲得檔案長度的方法。ContentLength是不很熟啊。它是http協議裡描述頭(head)部分的描述屬性之一。實際這裡是發送了一個http請求,分析了返回(response)裡資料內容。
我們常常會把檔案下載到手機的儲存卡裡,所以還會用到獲得儲存卡路徑的方法: <span xmlns="http://www.w3.org/1999/xhtml">// 獲得儲存卡路徑,構成 儲存檔案的目標路徑String dirName = "";dirName = Environment.getExternalStorageDirectory()+"/MyDownload/";File f = new File(dirName);if(!f.exists()){ f.mkdir();}</span>
Environment.getExternalStorageDirectory() 方法會返回一個字串,指示了儲存卡的路徑。我們拼接字串出一個準備存放下載檔案的檔案夾。並先判斷檔案夾是是否存在,如果不存在,則建立一個檔案夾。
做完了上面的準備後,基本就能實現下載了。我們看看主要的完整代碼。
下載前的準備工作: //要下載的檔案路徑String urlDownload = "";//urlDownload = "http://192.168.3.39/text.txt";urlDownload = "http://www.baidu.com/img/baidu_sylogo1.gif";// 獲得儲存卡路徑,構成 儲存檔案的目標路徑String dirName = "";dirName = Environment.getExternalStorageDirectory()+"/MyDownload/";File f = new File(dirName);if(!f.exists()){ f.mkdir();}
下載的操作: //準備拼接新的檔案名稱(儲存在儲存卡後的檔案名稱)String newFilename = _urlStr.substring(_urlStr.lastIndexOf("/")+1);newFilename = _dirName + newFilename;File file = new File(newFilename);//如果目標檔案已經存在,則刪除。產生覆蓋舊檔案的效果if(file.exists()){ file.delete();}try { // 構造URL URL url = new URL(_urlStr); // 開啟串連 URLConnection con = url.openConnection(); //獲得檔案的長度 int contentLength = con.getContentLength(); System.out.println("長度 :"+contentLength); // 輸入資料流 InputStream is = con.getInputStream(); // 1K的資料緩衝 byte[] bs = new byte[1024]; // 讀取到的資料長度 int len; // 輸出的檔案流 OutputStream os = new FileOutputStream(newFilename); // 開始讀取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完畢,關閉所有連結 os.close(); is.close(); } catch (Exception e) { e.printStackTrace();} 轉自:http://www.itivy.com/android/archive/2011/6/28/android-file-download.html
我的部分代碼如下://從伺服器下載圖片到本地 private void getPhotoFromServer(List<Advert> adverts){ //讀取伺服器ip地址 String serverip = NetworkUtil.getDns(); //直接讀取伺服器的ip地址 //String serverURL = serverURLAll.substring(serverURLAll.indexOf(":")+3, serverURLAll.lastIndexOf(":")); //最好還是直接從資料庫或者其他地方讀取ip地址 Log.d(TAG, "server ip is " + serverip); //整合成完整的廣告圖片存放地址目錄 String urlDownload = "http://" + serverip +":8080/proPicRecord/"; //廣告圖片本地sd卡存放目錄 String dirName = "/sdcard/advert/"; //判斷本地存放廣告圖片的目錄是否存,不存在則建立檔案夾 File f = new File(dirName); if(!f.exists()) { f.mkdir(); } //伺服器上廣告圖片完整路徑名,供下載用 String urlDownloadAll = ""; //準備拼接新的檔案名稱(儲存在儲存卡後的檔案名稱) for(int i=0; adverts!=null && i<adverts.size(); i++){ Advert advert = adverts.get(i); //下載路徑添加上廣告圖片名字 urlDownloadAll = urlDownload + advert.getUrl(); Log.d(TAG, "The whole server download url is " + urlDownloadAll); //擷取廣告圖片的名字,其實使用advert.getUrl()也可 String newFilename = urlDownloadAll.substring(urlDownloadAll.lastIndexOf("/")+1); //本地播放廣告圖片的完整路徑 newFilename = dirName + newFilename; Log.d(TAG, "The local url is" + newFilename); File file = new File(newFilename); //如果目標檔案已經存在,則刪除,產生覆蓋舊檔案的效果(此處以後可以擴充為已經存在圖片不再重新下載功能) if(file.exists()) { file.delete(); } try{ //構造URL URL url = new URL(urlDownloadAll); //開啟串連 URLConnection con = url.openConnection(); //獲得檔案的長度 //int contentLength = con.getContentLength(); //System.out.println("長度 :"+contentLength); //輸入資料流 InputStream is = con.getInputStream(); //1K的資料緩衝 byte[] bs = new byte[1024]; //讀取到的資料長度 int len; //輸出的檔案流 OutputStream os = new FileOutputStream(newFilename); //開始讀取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } //完畢,關閉所有連結 os.close(); is.close(); }catch(Exception e){ e.printStackTrace(); } } }