Breaking News
Home / Java / Java Snippets / How to download Image from URL and save it in Java?

How to download Image from URL and save it in Java?

Snippet

  1. import javax.imageio.ImageIO;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.URL;
  6.  
  7. public class ImageDownloader
  8. {      
  9.     public static void main(String[] args )
  10.     {
  11.         BufferedImage image =null;
  12.         try{
  13.  
  14.             URL url =new URL("http://developerfeed.com/sites/default/files
  15.                              /have_a_question.png");
  16.             // read the url
  17.            image = ImageIO.read(url);
  18.  
  19.             for png
  20.             ImageIO.write(image, "png",new File("/tmp/have_a_question.png"));
  21.  
  22.             // for jpg
  23.             ImageIO.write(image, "jpg",new File("/tmp/have_a_question.jpg"));
  24.  
  25.         }catch(IOException e){
  26.             e.printStackTrace();
  27.         }
  28.     }}

<!>[pb_builder]

Check Also

How to progamatically get the Java version?

Snippet The key here is to use the system property java.version and get minor and …

Advertisment ad adsense adlogger