Here you can find the source of downloadFile(String fileURL, String localFileName, String destinationDir)
public static void downloadFile(String fileURL, String localFileName, String destinationDir) throws IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; public class Main { public static void downloadFile(String fileURL, String localFileName, String destinationDir) throws IOException { OutputStream outStream = null; InputStream is = null;//from w w w . jav a 2 s. co m try { URL Url = new URL(fileURL); outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + '/' + localFileName)); URLConnection uCon = Url.openConnection(); is = uCon.getInputStream(); int byteRead = 0; byte[] buf = new byte[10000]; while ((byteRead = is.read(buf)) != -1) { outStream.write(buf, 0, byteRead); } } finally { if (is != null) { is.close(); } if (outStream != null) { outStream.close(); } ; } } public static void downloadFile(String fileURL, String destinationDir) throws IOException { int slashIndex = fileURL.lastIndexOf('/'); int periodIndex = fileURL.lastIndexOf('.'); String fileName = fileURL.substring(slashIndex + 1); if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fileURL.length() - 1) { downloadFile(fileURL, fileName, destinationDir); } else { System.err.println("path or file name."); } } }