Skip to main content

Download a file over HTTPS using Apache Commons' FileUtils

Published: 2019-01-30 | Lastmod: 2019-05-21

In some of our selenium tests, we download a PDF file generated by our app and then inspect it. This works fine locally since the app URL is http. But in our CI environment, everything is over https. So we had to do some tweaking before calling FileUtils.copyURLToFile. Below is a stripped down version of our solution:

// Create a new trust manager that trusts all certificates
TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        public[] getAcceptedIssuers() {
            return null;

        public void checkClientTrusted(
      [] certs, String authType) {

        public void checkServerTrusted(
      [] certs, String authType) {

// Activate the new trust manager
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new;

// Open connection to the URL
URL url = new URL("");
URLConnection connection = url.openConnection();

// Download the file 
FileUtils.copyURLToFile(connection.getURL(), new File("/path/to/file"));

Credits to these stackoverflow answers: