douhanzhuo6905 2015-05-26 14:04 采纳率: 100%
浏览 59

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

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

  • 写回答

1条回答 默认 最新

  • dongyili5843 2015-05-26 17:04
    关注

    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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题