Here you can find the source of download(String fileUrl, String savePath, String fileName)
public static void download(String fileUrl, String savePath, String fileName)
//package com.java2s; //License from project: Apache License import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Main { public static void download(String fileUrl, String savePath, String fileName) { OutputStream out = null;//from w w w . ja va2s .co m DataInputStream input = null; try { File directory = new File(savePath); if (!directory.exists() && !directory.isDirectory()) { directory.mkdirs(); } if (!checkFileExists(fileUrl)) { return; } URL url = new URL(fileUrl); input = new DataInputStream(url.openStream()); File downFile = new File(savePath + fileName); if (downFile.exists() && downFile.isFile()) { downFile.delete(); } out = new FileOutputStream(downFile); byte[] buffer = new byte[1024]; int length = -1; while ((length = input.read(buffer)) != -1) { out.write(buffer, 0, length); } buffer = null; } catch (MalformedURLException e) { } catch (IOException e) { } finally { try { if (input != null) { input.close(); } if (out != null) { out.close(); } } catch (IOException e) { } } } public static boolean checkFileExists(String fileUrl) { boolean exist = false; try { URL url = new URL(fileUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); int code = connection.getResponseCode(); if (code == 200) { exist = true; } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return exist; } }