1
我試圖讓android從我的web服務器上使用php頁面下載一個zip文件來下載它。Android下載Zip(可以更改服務器上的名稱)
如果我下載使用靜態鏈接到zip文件,它的偉大工程,但我試圖使用PHP文件,使用此代碼下載:
function file_list($d,$x){
foreach(array_diff(scandir($d,1),array('.','..')) as $f)if(is_file($d.'/'.$f)&&(($x)?ereg($x.'$',$f):1))$l[]=$f;
return $l;
}
$arr = file_list("../download/",".zip");
$filename = $arr[0];
$filepath = "../download/".$arr[0];
if(!file_exists($filepath)){
die('Error: File not found.');
} else {
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($filepath);
}
如果我訪問了PHP文件我瀏覽器,它下載的zip很棒!現在
,在Android方面,它就像下載它應該而且拉鍊的名稱是getZip.php(PHP文件名),文件大小爲22
這是做下載代碼在Android上的東西
int count;
try {
downloadCoords();
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lengthOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Length of file: " + lengthOfFile);
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
if(!f.isDirectory()){
f.mkdirs();
}
Uri u = Uri.parse(url.toString());
File uf = new File(""+u);
zipname = uf.getName();
Log.d("ANDRO_ASYNC", "Zipname: " + zipname);
File zipSDCARD = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+zipname);
if(!zipSDCARD.isFile()){
Log.d("zipSDCARD.isFile()","false");
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/" + zipname);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lengthOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
successDownload = true;
} catch (Exception e) {
successDownload = false;
Log.e("Error","DownloadZip",e);
}
需要做什麼,正確地獲得zipname和ziplength。
在此先感謝。
嗨,如果適用於你的情況,你可以使用[DownloadManager](http://developer.android.com/reference/android/app/DownloadManager.html)。這將處理所有下載的內容(如重試等)。 – Renard 2012-03-27 20:05:37
@Renard我真的不知道這是否會有所幫助,我認爲它會達到完全相同的效果...... – silentw 2012-03-27 21:00:58