简体   繁体   中英

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. I want to download the file which that script forces to download, not the script itself, using 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. 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? Here's my relevant php script:

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

and the java would be something like :

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 )

$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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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