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