DEV Community

Thomas Küstermann
Thomas Küstermann

Posted on

Downloading Files With Groovy and Authentication

Downloading a file from a URL requiring user authentication with Groovy is as easy as:

java.net.Authenticator.setDefault (new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password.toCharArray());
    }
});

destinationFile.withOutputStream { it << new URL(downloadUrl).newInputStream() }

Links

Top comments (0)