我的服务器上有 php 脚本,当给出正确的 key 时,该脚本会强制下载。我想使用 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 。不要尝试强制下载,让用户或他们的偏好决定如何处理文件。可以使文件类型更通用,以便浏览器更频繁地提示:
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;
关于java - 如何从java中的php脚本强制下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30460878/