Skip to content

How to convert https url to File in Java?

I have an https url which is of String format. How can I convert it to File? I tried this but I’m getting error – URI scheme is not "file"

 public static File convertURLToFile(String strURL) throws MalformedURLException {
        URL url = new URL(strURL);
        File file;
        try {
            file = new File(url.toURI());
        } catch (URISyntaxException e) {
            String message = "Error in converting url to file";
            throw new RuntimeException(message, e);
        }
        return file;
    }

Answer

FileUtils.copyURLToFile worked well for me.

Reference – https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#copyURLToFile-java.net.URL-java.io.File-