105

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
2
  • Could you elaborate? What are you trying to achieve? Commented Nov 30, 2011 at 11:14
  • I would also recommend looking at this question and check if it does the job for you Commented Nov 30, 2011 at 11:15

5 Answers 5

95

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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);
@AdrienBe FileUtils gets an error in Eclipse, it doesn't show an import.
import org.apache.commons.io.FileUtils should do the job, see link to library in answer
And org.apache.commons.io.FileUtils.toFile(URL url) in case we just want to convert URL to File.
Note: add implementation 'commons-io:commons-io:2.6' to gradle dependencies block
69

Since Java 7

File file = Paths.get(url.toURI()).toFile();

3 Comments

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
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.
Spaces should be replaced by: %20
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.

5 Comments

Is their any way to create file object from BufferedImage with out writing it to disk
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.
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.
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
this saved my day after more than 4 hrs of debugging. Thanks
16

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());

4 Comments

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
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()
Neither getFile or getPath does proper decoding, so the Paths.get() approach just above is preferable,
@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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.