How to download a file over HTTPs in groovy?

At present I have a code which is able to download a file over HTTP in java/groovy, need some help to configure the same code to download any file over HTTPs too.

public def download(def downloadUrl,def downloadPath) 
{
    URL link = new URL(downloadUrl) 

    try {
        InputStream inputstream = new BufferedInputStream(link.openStream())
        ByteArrayOutputStream out = new ByteArrayOutputStream()
        byte[] buf = new byte[1024]
        int n = 0
        while (-1!=(n=inputstream.read(buf))) {
            out.write(buf, 0, n)
        }
        out.close()
        inputstream.close()
        byte[] response = out.toByteArray()

        FileOutputStream fos = new FileOutputStream(downloadPath)
        fos.write(response)
        fos.close()

        println("Download complete for ["+downloadPath+"]")

    } catch(Exception e) {
        println("Exception occured " + e.getMessage(),e)
        return false
    }

    return true
}

static main(args) {
    Downloader downloader = new Downloader()
    println downloader.download("http://www.7-zip.org/a/7za920.zip", "c:7za920.zip")
}

I went through this link but still I get below exception

Exception occured sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target


Source: java

Leave a Reply