简体   繁体   English

如何下载强制从Java中的php脚本下载的文件?

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

I have php script on my server which forces a download when a correct key is given. 我的服务器上有php脚本,当给出正确的密钥时会强制下载。 I want to download the file which that script forces to download, not the script itself, using java. 我想使用Java下载该脚本强制下载的文件,而不是脚本本身。 I see questions which answer how to download a file which is located at a certain url, but not how to download a file forced through a script. 我看到一些问题,这些问题回答了如何下载位于某个URL的文件,而不是如何下载通过脚本强制执行的文件。 Also would it be possible to route the download through a certain path instead of having it go to the downloads folder of the computer, or wherever it would automatically go? 还有可能通过某个路径来路由下载,而不是将其转到计算机的downloads文件夹,或者自动将其转到何处? Here's my relevant php script: 这是我相关的php脚本:

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

and the java would be something like : 和Java将是这样的:

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

but then i don't know where to go from there. 但是后来我不知道从那里去哪里。 Thanks 谢谢

I suspect the issue is here: 我怀疑问题在这里:

echo $strFile;

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

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

I would also offer headers that fit the bill better for each file type. 我还将提供更适合每种文件类型的标题。 Don't try to force a download, let the user or their preference determine ho9w to handle the file. 不要尝试强制下载,而要让用户或其偏好确定ho9w处理该文件。 Can make the file type more general so the browser will prompt more often: 可以使文件类型更通用,以便浏览器更频繁地提示:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM