首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用powershell下载jdk

使用powershell下载jdk
EN

Stack Overflow用户
提问于 2014-06-26 20:08:20
回答 5查看 13.7K关注 0票数 6

我正在尝试使用powershell脚本下载java jdk,如下所示。

http://poshcode.org/4224

。在这里,正如作者所指定的,如果我改变出现最新jdk的源url,即,

http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-x64.exe

没有加载内容,只有大约6KB的内容被下载。我有一个疑问,powershell脚本的下载限制是不是只有6KB?

代码如下:

代码语言:javascript
复制
$source = "http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-i586.exe"
      $destination = "C:\Download\Java\jdk-7u60-windows-i586.exe"
      $client = new-object System.Net.WebClient
      $client.DownloadFile($source, $destination)
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-06-26 21:07:48

在检查oracle站点上的会话时,下面的cookie引起了注意:oraclelicense=accept-securebackup-cookie。考虑到这一点,您可以运行以下代码:

代码语言:javascript
复制
$source = "http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-i586.exe"
$destination = "C:\Download\Java\jdk-7u60-windows-i586.exe"
$client = new-object System.Net.WebClient 
$cookie = "oraclelicense=accept-securebackup-cookie"
$client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie) 
$client.downloadFile($source, $destination)
票数 10
EN

Stack Overflow用户

发布于 2019-04-11 03:42:25

在需要将JDK作为依赖项的平台安装程序需要此脚本后:

代码语言:javascript
复制
<# Config #>
$DownloadPageUri = 'https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html';
$JavaVersion = '8u201';

<# Build the WebSession containing the proper cookie 
   needed to auto-accept the license agreement. #>
$Cookie=New-Object -TypeName System.Net.Cookie; 
$Cookie.Domain='oracle.com';
$Cookie.Name='oraclelicense';
$Cookie.Value='accept-securebackup-cookie'; 
$Session= `
   New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession;
$Session.Cookies.Add($Cookie);

<# Fetch the proper Uri and filename from the webpage. #>
$JdkUri = (Invoke-WebRequest -Uri $DownloadPageUri -WebSession $Session -UseBasicParsing).RawContent `
    -split "`n" | ForEach-Object { `
        If ($_ -imatch '"filepath":"(https://[^"]+)"' { `
            $Matches[1] `
        } `
    } | Where-Object { `
        $_ -like "*-$JavaVersion-windows-x64.exe" `
    }[0];
If ($JdkUri -imatch '/([^/]+)$') { 
    $JdkFileName=$Matches[1];
}

<# Use a try/catch to catch the 302 moved temporarily 
   exception generated by oracle from the absance of
   AuthParam in the original URL, piping the exception's
   AbsoluteUri to a new request with AuthParam returned
   from Oracle's servers.  (NEW!) #>

try { 
    Invoke-WebRequest -Uri $JdkUri -WebSession $Session -OutFile "$JdkFileName"
} catch { 
    $authparam_uri = $_.Exception.Response.Headers.Location.AbsoluteUri;
    Invoke-WebRequest -Uri $authparam_uri -WebSession $Session -OutFile "$JdkFileName"
} 

在PowerShell 6.0中工作(你好,2019!)需要对正则表达式和-imatch行扫描发生的方式进行较小的修正。遵循302重定向的变通方法位于此处:https://github.com/PowerShell/PowerShell/issues/2896 (302重定向变通变通方法由fcabralpacheco使Oracle下载再次工作!)

本方案自动下载Windows x64版8u201。

票数 1
EN

Stack Overflow用户

发布于 2018-12-03 10:28:22

由于接受的评论不再起作用,我无法发表评论,我将把我的方法留在这里。我修改了@steve-coleman的答案,让它扫描页面上以https而不是http开头的下载链接。此外,我还需要修改$Response,使其具有-UseBasicParsing标志,因为我使用的是无头Windows Server。总而言之,我使用的脚本是:

代码语言:javascript
复制
$DownloadPageUri = 'http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html'
$JavaVersion = '8u192'

$Response = Invoke-WebRequest -Uri $DownloadPageUri -UseBasicParsing

$JreUri = @($Response.RawContent -split "`n" | ForEach-Object {If ($_ -imatch '"filepath":"(https://[^"]+)"') {$Matches[1]}} | Where-Object {$_ -like "*-$JavaVersion-windows-x64.tar.gz"})
If (-Not $JreUri.Count -eq 1) {
    throw ('Expected to retrieve only one URI but got {0}' -f $JreUri.Count)
}

If ($JreUri[0] -imatch '/([^/]+)$') {
    $JreFileName = $Matches[1]
}
$JreFileName = 'jre-windows-x64.tar.gz'

If (-Not (Test-Path -Path "$PSScriptRoot\$JreFileName") -Or -Not (Test-Path -Path "$PSScriptRoot\Java-$JavaVersion.tag")) {
    $Cookie  = New-Object -TypeName System.Net.Cookie
    $Cookie.Domain = 'oracle.com'
    $Cookie.Name   = 'oraclelicense'
    $Cookie.Value  = 'accept-securebackup-cookie'
    $Session = New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession
    $Session.Cookies.Add($Cookie)
    Invoke-WebRequest -Uri $JreUri[0] -WebSession $Session -OutFile "$PSScriptRoot\$JreFileName"
    New-Item -Path "$PSScriptRoot\Java-$JavaVersion.tag" -ItemType File | Out-Null
}

像往常一样,YMMV,特别是如果甲骨文改变了他们的网站。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24430141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档