2012-03-27 58 views
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。

在此先感谢。

+0

嗨,如果适用于你的情况,你可以使用[DownloadManager](http://developer.android.com/reference/android/app/DownloadManager.html)。这将处理所有下载的内容(如重试等)。 – Renard 2012-03-27 20:05:37

+0

@Renard我真的不知道这是否会有所帮助,我认为它会达到完全相同的效果...... – silentw 2012-03-27 21:00:58

回答

0

嗯,我解决它使用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 = "http://".$_SERVER['SERVER_NAME']."/download/".$arr[0]; 
print($filepath); 

然后在Android上,我用一个BufferedReader来获得正确的链接:

private String getZipURL(){ 
    String result = ""; 
    InputStream is = null; 
    try{ 
     String url = ZipURL; 
     HttpPost httppost = new HttpPost(url); 
     HttpParams httpParameters = new BasicHttpParams(); 

     int timeoutConnection = 3000; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 

     int timeoutSocket = 3000; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

     DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters); 

     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    }catch(Exception e){ 
     Log.e("getZipURL", "Error in http connection "+e.toString()); 
     return null; 
    } 

    try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 

     result=sb.toString(); 
     return result; 
    }catch(Exception e){ 
     Log.e("convertZipURL", "Error converting result "+e.toString()); 
     return null; 
    } 
} 

它可能看起来不对,但它工作正常。我发布了代码,以便我可以为有相同问题的人解决问题。谢谢!

相关问题