2012-03-27 37 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; 
    } 
} 

它可能看起來不對,但它工作正常。我發佈了代碼,以便我可以爲有相同問題的人解決問題。謝謝!

相關問題