Skip to content

Instantly share code, notes, and snippets.

@oneyoung
Created April 15, 2015 05:08
Show Gist options
  • Save oneyoung/c0e9f613caac8ad74f35 to your computer and use it in GitHub Desktop.
Save oneyoung/c0e9f613caac8ad74f35 to your computer and use it in GitHub Desktop.
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