2012-03-27 3 views
1

私は、ダウンロードするために私のWebサーバーからzipファイルをダウンロードしようとしています。AndroidダウンロードZip(サーバー上の名前を変更することができます)

私はzipファイルへの静的リンクを使用してダウンロードする場合は、それは素晴らしい作品が、私はこのコードでPHPファイル使用してダウンロードしようとしている:私は私の中で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); 
} 

をブラウザ、それはちょうど素晴らしい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

+0

@レナード私は本当に助けになるかどうかわかりませんが、まったく同じことを達成すると思います... – silentw

答えて

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); 

次にアンドロイド上で、私は正確にリンクを取得するために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; 
    } 
} 

これは間違っているように見えますが、私が望むように動作します。私は同じ問題を抱えている人のための回避策を得ることができるようにコードを掲載しました。ありがとう!