1

私はアプリケーションを作成します。これは、ファイル形式のサーバーをダウンロードできます。

このコードを使用 >>>

public int startDownload(String url, String filename) {
// create url connector
URL u;
byte[] buffer = new byte[1024];
try {
  u = new URL(url + filename);
  HttpURLConnection c = (HttpURLConnection) u.openConnection();
  c.setRequestMethod("GET");
  c.setDoOutput(true);
  c.connect();
  InputStream in = c.getInputStream();
  m_lMaxDownloadSz = c.getContentLength();
  FileOutputStream f = new FileOutputStream(new File(FILE_PATH, filename));

  m_bCancelDownload = false;
  m_lCurrentDownloadSz = 0;
  int len = 0;
  while ((len = in.read(buffer, 0, 1024)) > 0) {

    // if download is canceled.
    if (m_bCancelDownload) {
      f.close();
      c.disconnect();
      return FILE_DOWNLOAD_CANCELED;
    }
    if (knot++ >= PROGRESS_STEP) {
      knot = 0;
      myProgressDialog.setProgress(GetDownloadStatus());
    }
    f.write(buffer, 0, len);
    m_lCurrentDownloadSz += len;
  }

  f.close();
  c.disconnect();

} catch (Exception e) {
  return FILE_DOWNLOAD_FAILED;
}

if (GetDownloadStatus() == 100) {
  return FILE_DOWNLOAD_FINISHED;
} else {
  return FILE_DOWNLOAD_FAILED;
}

}

そして、PHP強制ダウンロードで使いたいのですが、うまくいきません。通常、「app/aaa.apk」のようなファイルパスで使用するとうまくいきます!、そして、「php/forcedl.php」のようなPHPファイルに変更しても機能しません。

php force download を使用する必要がありますが、どのように使用すればよいですか?

ps。私は英語のスキルがほとんどありません。なぜなら、英語は私の主要な言語ではないからです

ありがとうございました

4

2 に答える 2

4

私は私の答えのためにアンドロイドを発見します-Javaコード__example:

        byte[] buffer = new byte[1024];         
        String url = "http://www.bla-bla.com/forcedownload.php"


            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                filesize = execute.getEntity().getContentLength();
                fileOutput = new FileOutputStream(new File(FILE_PATH, "file_copyformserver.apk"));

                 while ((len = content.read(buffer, 0, 1024)) > 0) {
                     fileOutput.write(buffer, 0, len);
                     Thread.sleep(100);
                 }

                fileOutput.close();


            } catch (Exception e) {
                e.printStackTrace();
            }

phpファイル__example:

      $file = '/home/bla-bla/domains/bla-bla.com/file/file.apk'; //not public folder
      if (file_exists($file)) {
         header('Content-Description: File Transfer');
         header('Content-Type: application/vnd.android.package-archive');
         header('Content-Disposition: attachment; filename='.basename($file));
         header('Content-Transfer-Encoding: binary');
         header('Expires: 0');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Pragma: public');
         header('Content-Length: ' . filesize($file));
         ob_clean();
         flush();
         readfile($file);
         exit;
     }

これはショートコードです。実行できない場合は申し訳ありません。:)

于 2011-10-04T21:09:44.677 に答える
0

これはおそらく最善の方法ではありませんが、ダウンロードに成功したときにファイルの名前を変更することを検討しましたか? 試したことはありませんがFile.renameTo()、Android の方法でできると思います。

動作すると思われる疑似コードを次に示しますが、今すぐ試すことはできません。

File.renameTo(new File(FILE_PATH, filename.replace(".apk", ".php")));
于 2011-09-08T12:34:55.620 に答える