cURL 下载文件问题

cURL download files issue

当我在浏览器中输入 URL (http://192.168.150.41:8080/filereport/31779/json/) 时,它会自动将文件下载为 31779_report.json

我正在尝试使用 curl 下载文件,但出现以下错误。

$ curl -O http://192.168.150.41:8080/filereport/31779/json/
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information

使用“-L”开关时,显示了 JSON 内容,但未保存文件。

$curl -L http://192.168.150.41:8080/filereport/31779/json/

{

.....
.....

}

如何使用 cURL / wget 下载确切的文件“31779_report.json”?

我不想将内容手动重定向 (>) 到文件 (31779_report.json)。

有什么建议吗?

curl 的 -O 标志尝试使用文件的远程名称,但是因为您的 URL 模式不以文件名结尾,所以它不能这样做。 -o 标志(小写 o)可用于手动指定文件名,而无需像这样重定向 STDOUT:

curl <address> -o filename.json

您可以使用 awk 手动构造您想要的文件名格式。例如:

URL=http://192.168.150.41:8080/filereport/31779/json/
file_number=$(echo $URL | awk -F/ '{print $(NF-2)}')
file_name="${file_number}_report.json"
curl -L "$URL" -o "$file_name"

希望这对您有所帮助。

wget --content-disposition 对我有用 (https://askubuntu.com/a/77713/18665)

$ wget --content-disposition https://www.archlinux.org/packages/core/x86_64/lib32-glibc/download/
...
Saving to: 'lib32-glibc-2.33-4-x86_64.pkg.tar.zst'

与卷曲比较:

$ curl -LO https://www.archlinux.org/packages/core/x86_64/lib32-glibc/download/
curl: Remote file name has no length!
curl: (23) Failed writing received data to disk/application

没有--content-disposition的wget:

$ wget https://www.archlinux.org/packages/core/x86_64/lib32-glibc/download/
...
Saving to: 'index.html'