我创建了一个应用程序,这可以下载文件格式服务器。
使用此代码>>>
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文件,如'php / forcedl.php',它不起作用。
我需要使用php force下载,我该如何使用?
PS。我的英语能力很差,因为英语不是我的主要语言
谢谢
答案 0 :(得分: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;
}
这是短代码,抱歉如果无法运行。 :)
答案 1 :(得分:0)
这可能不是最好的方法,但您是否考虑过成功下载后重命名文件?我没有尝试过,但我相信你可以使用Android中的File.renameTo()
方法来做到这一点。
这里有一些伪代码,我认为它会起作用,但现在不能尝试:
File.renameTo(new File(FILE_PATH, filename.replace(".apk", ".php")));