Java URL Download downloadImg(String urlPath, File file)

Here you can find the source of downloadImg(String urlPath, File file)

Description

download Img

License

Apache License

Declaration

public static void downloadImg(String urlPath, File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

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

import java.net.URL;

public class Main {
    public static void downloadImg(String urlPath, File file) throws IOException {

        URL url = new URL(urlPath);
        try (InputStream in = new BufferedInputStream(url.openStream());
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                FileOutputStream fos = new FileOutputStream(file);) {
            byte[] buf = new byte[1024];
            int n = 0;
            while (-1 != (n = in.read(buf))) {
                out.write(buf, 0, n);//  w  ww.j  a  v a2s  .c  om
            }
            byte[] bytes = out.toByteArray();
            fos.write(bytes);
        }
    }
}

Related

  1. downloadFromURL(String url)
  2. downloadFromUrl(String urlString, PrintStream logger)
  3. downloadFromUrl(URL url, String localFilename)
  4. downloadGzipCompressedFile(URL url, File destination)
  5. downloadHttp(String downloadURL, String basePath, String commonPathURL)
  6. downloadMobiFromEpubUrl(String href)
  7. downloadPageSource(String stringURL)
  8. downloadSource(PrintStream out, PrintStream err, String srcURL, String dirStr, boolean extract)
  9. downloadString(String url)