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
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
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.
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);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 answerorg.apache.commons.io.FileUtils.toFile(URL url) in case we just want to convert URL to File.implementation 'commons-io:commons-io:2.6' to gradle dependencies blockSince Java 7
File file = Paths.get(url.toURI()).toFile();
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. thanksfile:// protocol.%20You 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.
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.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. :PYou 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());
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