wget和curl下载JDK

时间:2022-12-25 15:15:33

有时候我们需要在服务器上安装Oracle JDK,这时候我们发现直接复制网页上面的地址是不能下载的

以下载jdk-8u101-linux-i586.tar.gz为例,右键查看到的地址为http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz,但是这个地址并不能直接下载

通过浏览器开发工具观察我们发现,下载时经过了3次重定向才拿到最终的下载地址,并且Cookies中有个参数必须带上

wget和curl下载JDK

将这几个302跳转复制成curl观察一下

第一次重定向

curl "http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Host: download.oracle.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" -H "Cookie: s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2Fwww.oracle.com"%"2Ftechnetwork"%"2Fjava"%"2Fjavase"%"2Fdownloads"%"2Fjdk8-downloads-2133151.html; s_sq="%"5B"%"5BB"%"5D"%"5D; oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive"
第二次重定向

curl "https://edelivery.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Host: edelivery.oracle.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" -H "Cookie: ARU_LANG=US; s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2Fwww.oracle.com"%"2Ftechnetwork"%"2Fjava"%"2Fjavase"%"2Fdownloads"%"2Fjdk8-downloads-2133151.html; s_sq="%"5B"%"5BB"%"5D"%"5D; oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive"
第三次重定向

curl "https://edelivery.oracle.com/osdc-otn/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Host: edelivery.oracle.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" -H "Cookie: ARU_LANG=US; s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2Fwww.oracle.com"%"2Ftechnetwork"%"2Fjava"%"2Fjavase"%"2Fdownloads"%"2Fjdk8-downloads-2133151.html; s_sq="%"5B"%"5BB"%"5D"%"5D; oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive"
真实下载

curl "http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz?AuthParam=1472300350_2a4acf9c53c18478db4839cbf7ec69c6" -H "Host: download.oracle.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" -H "Cookie: s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2Fwww.oracle.com"%"2Ftechnetwork"%"2Fjava"%"2Fjavase"%"2Fdownloads"%"2Fjdk8-downloads-2133151.html; s_sq="%"5B"%"5BB"%"5D"%"5D; oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive"
其中真实下载地址中的AuthParam每次都会变化。

curl要通过-O参数才能下载文件,否则只是将内容输出到控制台。

所以curl方式的下载命令为

curl -L "http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Cookie: oraclelicense=accept-securebackup-cookie"  -H "Connection: keep-alive" -O
其中-L参数能够追踪重定向的地址,不加-L只能返回302的结果,cookies中有用的内容是同意协议那一部分,Connection: keep-alive这句要加上,否则下载速度会变成蜗牛

执行结果如下

[root@mycentos ~]# curl -L "http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Cookie: oraclelicense=accept-securebackup-cookie"  -H "Connection: keep-alive" -O
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
8 174M 8 14.1M 0 0 404k 0 0:07:23 0:00:35 0:06:48 463k

同样的wget方式的下载命令为,wget的好处可以自动重定向,不用我们操心

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
其中 --no-check-certificate表示不校验SSL证书,因为中间的两个302会访问https,会涉及到证书的问题,不校验能快一点,影响不大,另一个--no-cookies表示不使用cookies,当然首次在header里指定的会带上,后面重定向的就不带了,这个影响也不大,可以不加。

[root@mycentos downloads]# wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
--2016-08-27 20:47:52--  http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
Resolving download.oracle.com... 206.160.170.51, 206.160.170.9
Connecting to download.oracle.com|206.160.170.51|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz [following]
--2016-08-27 20:47:52--  https://edelivery.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
Resolving edelivery.oracle.com... 60.254.134.143
Connecting to edelivery.oracle.com|60.254.134.143|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/osdc-otn/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz [following]
--2016-08-27 20:47:53--  https://edelivery.oracle.com/osdc-otn/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
Reusing existing connection to edelivery.oracle.com:443.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz?AuthParam=1472302193_c5af865490ac9f21f2605a3abee94745 [following]
--2016-08-27 20:47:53--  http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz?AuthParam=1472302193_c5af865490ac9f21f2605a3abee94745
Connecting to download.oracle.com|206.160.170.51|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 183456778 (175M) [application/x-gzip]
Saving to: “jdk-8u101-linux-i586.tar.gz.1”

 2% [==>                                   ] 4,740,512    863K/s  eta 4m 40s