如何使用批处理文件下载和安装JDK?

时间:2017-05-03 07:00:31

标签: batch-file

我需要编写一个脚本来检查客户端计算机中的java版本,并通过从Oracles网站下载最新的JDK来更新JDK。请帮助批处理文件。

2 个答案:

答案 0 :(得分:1)

无法仅使用批量从网站下载文件。纯批次没有网络功能。如果文件在FTP服务器上可用,则可以使用内置的FTP工具进行下载,但前提是服务器支持FTP活动模式。否则,您将不得不使用不同的脚本语言。以下是通过FTP或HTTP下载文件的PowerShell脚本的示例:

function downloadFile($url, $targetFile)
{
    "Downloading $url"
    $uri = New-Object "System.Uri" "$url"
    $request = [System.Net.HttpWebRequest]::Create($uri)
    $request.set_Timeout(15000) #15 second timeout
    $response = $request.GetResponse()
    $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
    $responseStream = $response.GetResponseStream()
    $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
    $buffer = new-object byte[] 10KB
    $count = $responseStream.Read($buffer,0,$buffer.length)
    $downloadedBytes = $count
    while ($count -gt 0)
    {
        [System.Console]::CursorLeft = 0
        [System.Console]::Write("Downloaded {0}K of {1}K", [System.Math]::Floor($downloadedBytes/1024), $totalLength)
        $targetStream.Write($buffer, 0, $count)
        $count = $responseStream.Read($buffer,0,$buffer.length)
        $downloadedBytes = $downloadedBytes + $count
    }
    "`nFinished Download"
    $targetStream.Flush()
    $targetStream.Close()
    $targetStream.Dispose()
    $responseStream.Dispose()
}

downloadFile "<url to the file to download>" "<local path where to save the file>"

您可以将此代码保存在.ps1文件中,然后从批处理代码中调用它。

您还可以使用curl之类的工具。它还允许您下载文件。

答案 1 :(得分:0)

尝试这样做

powershell -Command "Invoke-WebRequest  https://javadl.oracle.com/webapps/download/AutoDL?BundleId=236886_42970487e3af4f5aa5bca3f542482c60 -OutFile jdk8.exe"

jdk8.exe /s