2011-09-08 156 views
1

我做了一个应用程序,这可以下载文件格式的服务器。如何使用PHP强制下载在Android上下载文件

使用此代码>>>

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力下载使用,但它不能正常工作,一般它像“应用程序/ aaa.apk文件路径中使用' 这行得通! ,我改变为PHP文件,如'php/forcedl.php'它不起作用。

我需要用php强制下载,我该如何使用?

ps。我有一点英语语言技能,使英语不是我的主要语言

谢谢

+0

如果您尝试在浏览器中打开此URL,会发生什么情况? –

+0

我在android浏览器中打开www.xxx.com/x.php,它显示提交以下载file.apk ps。在文件php>标头强制下载到一个apk文件 – chawanan

回答

2

我发现我的回答 Android的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; 
    } 

这是短代码,对不起,如果不能运行。 :)

0

这也许是不这样做的最佳方式,但你有没有考虑重新命名后成功下载它的文件吗?我还没有尝试过,但我相信你可以在Android中使用File.renameTo()方法。

下面是一些伪代码,我认为会的工作,不能试一下,现在虽然:

File.renameTo(new File(FILE_PATH, filename.replace(".apk", ".php"))); 
+0

不能使用重命名 _________ 双向下载_____ 1. android应用程序。 - > .apk ______ 2. android app。 - > .PHP * - > .apk文件 __ * .PHP将读取的apk文件(PHP的力量下载) ____ 我想用第二种方式来下载文件 ____ 我使用的下载功能下载它。 PHP到我的设备不是.apk – chawanan

+0

我误解了你的问题,然后,对不起。 –