有时候我们需要在Linux服务器上安装Oracle JDK,这时候我们发现直接复制网页上面的地址是不能下载的
以下载jdk-8u101-linux-i586.tar.gz为例,右键查看到的地址为Java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz,但是这个地址并不能直接下载
通过浏览器开发工具观察我们发现,下载时经过了3次重定向才拿到最终的下载地址,并且Cookies中有个参数必须带上
将这几个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: " -H "Cookie: s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2F"%"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: " -H "Cookie: ARU_LANG=US; s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2F"%"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: " -H "Cookie: ARU_LANG=US; s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2F"%"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: " -H "Cookie: s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2F"%"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