Created
April 15, 2015 05:08
-
-
Save oneyoung/c0e9f613caac8ad74f35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 DownloadFile { | |
public static void download(String fileName, String url) throws IOException { | |
URL link = new URL(url); //The file that you want to download | |
//Code to download | |
InputStream in = new BufferedInputStream(link.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 = new FileOutputStream(fileName); | |
fos.write(response); | |
fos.close(); | |
//End download code | |
System.out.println("Finished"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment