Skip to content

Instantly share code, notes, and snippets.

@reflexdemon
Created May 16, 2018 10:27
Show Gist options
  • Save reflexdemon/8499cfb9128546f6e945306e9dd8e01c to your computer and use it in GitHub Desktop.
Save reflexdemon/8499cfb9128546f6e945306e9dd8e01c to your computer and use it in GitHub Desktop.
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* The Class URLDownloader.
*/
public class Downloader {
/**
* The Constant BUFF_SIZE.
*/
static final int BUFF_SIZE = 2 * 1024 * 1024; // 2MB
private static final boolean verbose = true;
/**
* The Constant OUTPUTDIR.
*/
private static String outDir = ".";
/**
* Just in time logger util for debugging.
*
* @param string the string
*/
public static void log(final String string) {
if (verbose) {
System.out.println(new java.util.Date() + " " + string);
}
}
/**
* Main method.
*
* @param args the arguments
*/
public static void main(final String args[]) {
log("START:::");
if (args.length == 0) {
System.out.println("Usage:\n\t Downloader <URL-to-download>");
System.exit(1);
} else {
String url = args[0];
String fileName = getFileNameFromURL(url);
log("Using " + args[0] + " as URL.");
log("Using " + args[0] + " as URL.");
getResponse(url, fileName);
}
}
/**
* Make HTP Request.
*
* @param urlString the URL that needs to be invoked.
* @param filename the local file name that needs to be saved
* @return the response
*/
public static void getResponse(final String urlString, final String filename) {
if (null == filename) {
throw new RuntimeException(
"Missling filename to be saved. Or Try using getResponse(String urlString)");
}
FileOutputStream out = null;
InputStream in = null;
try {
final URL url = new URL(urlString);
log("Opening connection to " + urlString + "...");
final URLConnection urlC = url.openConnection();
// Copy resource to local file, use remote file
// if no local file name specified
in = url.openStream();
// Print info about resource
log("Copying resource (type: " + urlC.getContentType());
out = new FileOutputStream(filename);
final byte[] buffer = new byte[BUFF_SIZE];
int count = 0;
long size = 0;
while ((count = in.read(buffer, 0, buffer.length)) > 0) {
size += count;
out.write(buffer, 0, count);
}
log("Read..." + size + "..bytes, [" + size / 1024 + "KB]");
} catch (final MalformedURLException e) {
System.err.println(e.toString());
e.printStackTrace();
} catch (final IOException e) {
System.err.println(e.toString());
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (final IOException e) {
// Ignore
}
}
if (null != out) {
try {
out.close();
} catch (final IOException e) {
// Ignore
}
}
}
}
public static String getFileNameFromURL(String url) {
int slashIndex = url.lastIndexOf('/');
String filenameWithoutExtension = url.substring(slashIndex + 1);
return filenameWithoutExtension;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment