cURL下载文件问题

Aru*_*run 3 json curl wget

当我http://192.168.150.41:8080/filereport/31779/json/在浏览器中提供URL()时,它将自动将文件下载为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
Run Code Online (Sandbox Code Playgroud)

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

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

{

.....
.....

}
Run Code Online (Sandbox Code Playgroud)

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

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

有什么建议吗?

Jac*_*ken 5

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

curl <address> -o filename.json
Run Code Online (Sandbox Code Playgroud)

您可以使用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"
Run Code Online (Sandbox Code Playgroud)

希望这对您有所帮助。