100

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
nidhin
  • 6,661
  • 6
  • 32
  • 50
  • Could you elaborate? What are you trying to achieve? – MByD Nov 30 '11 at 11:14
  • I would also recommend looking at [this question](http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java) and check if it does the job for you – MByD Nov 30 '11 at 11:15

5 Answers5

94

Use Apache Common IO's FileUtils:

import org.apache.commons.io.FileUtils

FileUtils.copyURLToFile(url, f);

The method downloads the content of url and saves it to f.

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
gigadot
  • 8,879
  • 7
  • 35
  • 51
  • 16
    This worked for me: `URL url = new URL("http://www.mydomain.com/pathToFile/fileName.pdf"); String tDir = System.getProperty("java.io.tmpdir"); String path = tDir + "tmp" + ".pdf"; file = new File(path); file.deleteOnExit(); FileUtils.copyURLToFile(url, file);` – Adriano Jul 11 '13 at 06:58
  • @AdrienBe `FileUtils` gets an error in Eclipse, it doesn't show an import. – Galen Nare Nov 25 '13 at 22:45
  • 1
    `import org.apache.commons.io.FileUtils` should do the job, see link to library in answer – Adriano Nov 26 '13 at 07:54
  • And `org.apache.commons.io.FileUtils.toFile(URL url)` in case we just want to convert `URL` to `File`. – Kenston Choi Jan 03 '19 at 03:27
  • 1
    Note: add `implementation 'commons-io:commons-io:2.6'` to gradle dependencies block – Saeed Jan 05 '20 at 20:53
63

Since Java 7

File file = Paths.get(url.toURI()).toFile();
Asu
  • 1,723
  • 2
  • 21
  • 32
  • 6
    This is for retrieving the file location, with the `file://` protocol for exemple, not sure if the OP wanted the location or download the image itself, but anyways this is what I was looking for personally. thanks – User9123 Oct 19 '18 at 20:26
  • 1
    This causes a URISyntaxException if the URL has any spaces in it as might likely be the case if the URL uses a `file://` protocol. – Jack J May 22 '20 at 02:54
  • Spaces should be replaced by: `%20` – Asu May 26 '20 at 16:48
28

You can make use of ImageIO in order to load the image from an URL and then write it to a file. Something like this:

URL url = new URL("http://google.com/pathtoaimage.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("downloaded.jpg");
ImageIO.write(img, "jpg", file);

This also allows you to convert the image to some other format if needed.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • Is their any way to create file object from BufferedImage with out writing it to disk – nidhin Nov 30 '11 at 11:36
  • 1
    A file by definition needs to be stored on some file system. You would need some in-memory file system in order to end up with a file that is not persisted. – Costi Ciudatu Nov 30 '11 at 11:49
  • did you notice accepted answer ? i don't know FileUitls whether creates a temp file for that :P – nidhin Nov 30 '11 at 11:52
  • 1
    My feeling is that your requirement needs some second thought... Namely, assuming you need to call a method like `processImage(File f)`, this should be changed to `processImage(BufferedImage img)` or `processImage(InputStream is)`. Either of these changes would spare you of storing external images on the local FS. – Costi Ciudatu Nov 30 '11 at 11:56
  • Regarding the accepted answer, `FileUtils` needs a `File` instance (as you may have noticed), the same as the `ImageIO` API does. If you'll choose to create a temporary file, that will still be persisted, but deleted on JVM exit (and/or created in the system tmp directory). So from this point of view, the two answers are exactly the same. :P – Costi Ciudatu Nov 30 '11 at 12:02
  • this saved my day after more than 4 hrs of debugging. Thanks – Jose Mhlanga Jun 15 '20 at 07:34
15

You can convert the URL to a String and use it to create a new File. e.g.

URL url = new URL("http://google.com/pathtoaimage.jpg");
File f = new File(url.getFile());
byxor
  • 5,930
  • 4
  • 27
  • 44
Arthur bauer
  • 633
  • 7
  • 11
  • 9
    When using this method for local file system files that have a space in the full path, %20 will not get converted to space automatically and may cause an exception – Asu May 08 '17 at 15:17
  • url.getFile attaches the query (say, ?a=b&c=d), if any. Usually moot since there is usually no query. However, to get just the file, one should use url.getPath() – Zweibieren Nov 09 '18 at 20:03
  • Neither getFile or getPath does proper decoding, so the Paths.get() approach just above is preferable, – Zweibieren Nov 09 '18 at 22:04
  • @byxor -- since you've corrected your mistake, is there any value in keeping your two comments? If not, remove. I'll remove my comment when you do. – Jan Nielsen Mar 04 '22 at 19:02
14

In order to create a File from a HTTP URL you need to download the contents from that URL:

URL url = new URL("http://www.google.ro/logos/2011/twain11-hp-bg.jpg");
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("downloaded.jpg"));
byte[] buf = new byte[512];
while (true) {
    int len = in.read(buf);
    if (len == -1) {
        break;
    }
    fos.write(buf, 0, len);
}
in.close();
fos.flush();
fos.close();

The downloaded file will be found at the root of your project: {project}/downloaded.jpg

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
Liviu T.
  • 23,584
  • 10
  • 62
  • 58