Here you can find the source of download(String downloadUrlStr, String filename)
Parameter | Description |
---|---|
downloadUrlStr | download url |
filename | save file name(not path, file name only). |
public static void download(String downloadUrlStr, String filename) throws Exception
//package com.java2s; /**/* ww w.j ava2s . c o m*/ * API License */ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; public class Main { /** * download data from url. * * @param downloadUrlStr download url * @param filename save file name(not path, file name only). */ public static void download(String downloadUrlStr, String filename) throws Exception { File downloadDir = new File(new File(".", "download").getAbsolutePath()); if (!downloadDir.exists()) { downloadDir.mkdirs(); } File filepath = new File(downloadDir, filename); System.out.println("------------------------------------"); System.out.println("Start download."); System.out.println("DOWNLOAD_URL = " + downloadUrlStr); System.out.println("DOWNLOAD_FILE = " + filepath.getAbsolutePath()); System.out.println("------------------------------------"); InputStream is = null; FileOutputStream fos = null; try { URL downloadURL = new URL(downloadUrlStr); // get InputStream from download URL. is = downloadURL.openConnection().getInputStream(); // create file output stream fos = new FileOutputStream(filepath, false); // download int b; while ((b = is.read()) != -1) { fos.write(b); } } finally { if (is != null) { is.close(); } if (fos != null) { fos.close(); } } } }