nidhin nidhin - 4 months ago 55
Java Question

How to create file object from URL object

I need to create a File object from URL object
My requirement is
I need to create a file object of a web image (say googles logo)

URL url = new URL("http://google.com/pathtoaimage.jpg");
File f = create image from url object

Answer

Using FileUtils in Apache Common IO to simplify the problem:

import org.apache.commons.io.FileUtils

FileUtils.copyURLToFile(url, f);

The method downloads the URL, url, and save to file f.