Java URL Download downloadAndSaveImage(URL imageUrl, String path)

Here you can find the source of downloadAndSaveImage(URL imageUrl, String path)

Description

download And Save Image

License

Open Source License

Declaration

public static boolean downloadAndSaveImage(URL imageUrl, String path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;

public class Main {
    public static boolean downloadAndSaveImage(URL imageUrl, String path) {
        try {//  w ww. j  ava  2 s .  c  om
            InputStream in;
            in = new BufferedInputStream(imageUrl.openStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;
            while (-1 != (n = in.read(buf))) {
                out.write(buf, 0, n);
            }
            out.close();
            in.close();
            byte[] response = out.toByteArray();

            FileOutputStream fos;
            fos = new FileOutputStream(path);
            fos.write(response);
            fos.close();
        } catch (IOException e1) {
            e1.printStackTrace();
            return false;
        }
        return true;
    }
}

Related

  1. download(URL sourceUrl, File destinationFile)
  2. download(URL url)
  3. download(URL url, File destination)
  4. download(URL url, File file)
  5. downloadAndExtract(String fileURL, String targetDirectory)
  6. downloadAndUnzip(String url, File location)
  7. downloadAsString(String url)
  8. downloadBinary(URL BaseURL, String Name, File TargetDirectory)
  9. downloadData(String url, String params)