最近做一個項目需要從伺服器下載下傳圖檔到本地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();
}
}
}