1

我的服务器上有 php 脚本,当给出正确的密钥时会强制下载。我想使用java下载该脚本强制下载的文件,而不是脚本本身。我看到的问题回答了如何下载位于某个 url 的文件,但没有回答如何通过脚本强制下载文件。是否可以通过某个路径路由下载,而不是让它进入计算机的下载文件夹,或者它会自动去哪里?这是我的相关 php 脚本:

header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . str_replace(" ", "_", $arrRES['file']) . "\"");                    
echo $strFile;

和java会是这样的:

URL url= new URL("http://supremesharkbot.com:8080/update/?key="+activationkey);

但后来我不知道从那里去哪里。谢谢

4

1 回答 1

0

我怀疑问题出在这里:

echo $strFile;

我建议使用file_get_contents()http://php.net/manual/en/function.file-get-contents.php

$strFile = file_get_contents($arrRES['file']);
echo $strFile;

我还会为每种文件类型提供更符合要求的标题。不要试图强制下载,让用户或他们的偏好决定如何处理文件。可以让文件类型更通用,让浏览器更频繁地提示:

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . str_replace(" ", "_", $arrRES['file']) . "\"");
header("Content-Length: " . filesize($arrRES['file']));
$strFile = file_get_contents($arrRES['file']);
echo $strFile;
于 2015-05-26T17:04:49.120 回答