使用PowerShell下载jdk

use*_*915 5 java powershell powershell-2.0

我正在尝试使用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?

这是代码:

$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)
Run Code Online (Sandbox Code Playgroud)

Raf*_*Raf 10

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

$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)
Run Code Online (Sandbox Code Playgroud)


Mic*_*elS 5

编辑:这是您遇到问题的原因:您必须先接受条款,然后才能直接下载文件。

我正在使用以下脚本下载文件。它可以与HTTP以及FTP一起使用。这可能对您的任务有些大材小用,因为它还会显示下载进度,但是您可以对其进行调整,直到满足您的需求为止。

param(
    [Parameter(Mandatory=$true)]
    [String] $url,
    [Parameter(Mandatory=$false)]
    [String] $localFile = (Join-Path $pwd.Path $url.SubString($url.LastIndexOf('/'))) 
)

begin {
    $client = New-Object System.Net.WebClient
    $Global:downloadComplete = $false

    $eventDataComplete = Register-ObjectEvent $client DownloadFileCompleted `
        -SourceIdentifier WebClient.DownloadFileComplete `
        -Action {$Global:downloadComplete = $true}
    $eventDataProgress = Register-ObjectEvent $client DownloadProgressChanged `
        -SourceIdentifier WebClient.DownloadProgressChanged `
        -Action { $Global:DPCEventArgs = $EventArgs }    
}

process {
    Write-Progress -Activity 'Downloading file' -Status $url
    $client.DownloadFileAsync($url, $localFile)

    while (!($Global:downloadComplete)) {                
        $pc = $Global:DPCEventArgs.ProgressPercentage
        if ($pc -ne $null) {
            Write-Progress -Activity 'Downloading file' -Status $url -PercentComplete $pc
        }
    }

    Write-Progress -Activity 'Downloading file' -Status $url -Complete
}

end {
    Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged
    Unregister-Event -SourceIdentifier WebClient.DownloadFileComplete
    $client.Dispose()
    $Global:downloadComplete = $null
    $Global:DPCEventArgs = $null
    Remove-Variable client
    Remove-Variable eventDataComplete
    Remove-Variable eventDataProgress
    [GC]::Collect()    
}
Run Code Online (Sandbox Code Playgroud)

  • 但是现在我可以确定问题出在条款协议上。如果您保存错误的网站,它的大小恰好是您要下载的文件的大小。您甚至可以将下载的文件从.exe重命名为.html,然后在浏览器中将其打开。 (2认同)