Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create file object from URL object (image) [duplicate]

Tags:

java

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 
like image 708
nidhin Avatar asked Nov 30 '11 11:11

nidhin


People also ask

How to make file object from URL?

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

What is URL createObjectURL()?

createObjectURL() The URL. createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.

How do I create a Blob file?

To create File object from Blob with JavaScript, we can use the File constructor`. const file = new File([blob], "filename"); to create a File object from a blob by putting it in an array.


2 Answers

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.

like image 200
gigadot Avatar answered Sep 29 '22 12:09

gigadot


Since Java 7

File file = Paths.get(url.toURI()).toFile(); 
like image 32
Asu Avatar answered Sep 29 '22 12:09

Asu