当URL重定向时如何修复wget下载文件名

15

Wget 在处理301/302重定向时无法检测文件名。例如,从http://www.mysql.com/downloads/mirror.php?id=408580下载MySQL源代码时。

wget http://www.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.25a-linux2.6-x86_64.tar.gz/from/http://cdn.mysql.com/

该文件将保存为 index.html

wget http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.25a-linux2.6-x86_64.tar.gz

文件将保存为 mysql-5.5.25a-linux2.6-x86_64.tar.gz

但是第一个 URL 只是第二个 URL 的重定向,如下所示:

curl -I http://www.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.25a-linux2.6-x86_64.tar.gz/from/http://cdn.mysql.com/

HTTP/1.1 302 Found
Date: Mon, 09 Jul 2012 06:11:50 GMT
Server: Apache/2.2
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: mirror=http%3A%2F%2Fcdn.mysql.com%2F; expires=Sat, 08-Jul-2017 06:11:50 GMT; path=/
Location: http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.25a-linux2.6-x86_64.tar.gz
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8

如何使用wget自动将http://www.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.25a-linux2.6-x86_64.tar.gz/from/http://cdn.mysql.com/保存为".tar.gz"文件,而不使用-O参数?


请参阅Unix.SE问题。我的解决方案(在那里发布并解释了评论)是使用--trust-server-names - Adam Katz
啊,还有另一个关于这个问题的SO问题:请参见如何使用wget重命名下载的文件? - Adam Katz
1个回答

29

尝试以下命令:

wget --content-disposition http://www.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.25a-linux2.6-x86_64.tar.gz/from/http://cdn.mysql.com/

当此项设置为开启时,将启用对"Content-Disposition"头文件的实验性(不完全功能)支持。这可能会导致执行“HEAD”请求时向服务器发送额外的往返,并已知存在一些缺陷,因此默认情况下未启用。

此选项对于某些使用"Content-Disposition"头文件来描述下载文件名称的文件下载CGI程序非常有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接