Wednesday, December 16, 2015

Download an image locally

To copy any image you want that you found in internet, sure, right-click save-as. But what if you have a ton to do?

You can write a little java program. Boy it takes just 3 lines (or less if you jam everything in 1). These Files and Paths things are in java.nio.file package.

 public static void downloadLocalCopy(String localFile,String urlString) {
  try {
   URL url = new URL(urlString);
   try(InputStream in = url.openStream()){
   Files.copy(in, Paths.get(localFile));
   }
   
  }
   catch (Exception e) {
   e.printStackTrace();
  }

 }
Sample usage: downloadLocalCopy("mycopy.png","https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");

Yes, this came from a stackoverflow post.

No comments: