簡體   English   中英

如何下載強制從Java中的php腳本下載的文件?

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

我的服務器上有php腳本,當給出正確的密鑰時會強制下載。 我想使用Java下載該腳本強制下載的文件,而不是腳本本身。 我看到一些問題,這些問題回答了如何下載位於某個URL的文件,而不是如何下載通過腳本強制執行的文件。 還有可能通過某個路徑來路由下載,而不是將其轉到計算機的downloads文件夾,或者自動將其轉到何處? 這是我相關的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/zh/function.file-get-contents.php

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

我還將提供更適合每種文件類型的標題。 不要嘗試強制下載,而要讓用戶或其偏好確定ho9w處理該文件。 可以使文件類型更通用,以便瀏覽器更頻繁地提示:

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