如何在java中下载强制从php脚本下载的文件


How to download a file forced to download from a php script in java?

我的服务器上有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);

但我不知道从那里去哪里。由于

我怀疑问题在这里:

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;