Friday, September 14, 2007

Java Downloader Code + Proxy

For those that read the previous post Java Downloader Code, would maybe like to read this post as I have smoothed it out a bit and found out how to have Proxy support.

I have now shaped the code to be more like a Java commandline application downloader. For the proxy settings, you can go to the Windows Control Panel and look for the Java icon, and once opening that control panel app, you can specify the Proxy settings for Java to use and based on that, this downloader will load that settings and use it to navigate through a proxy server. I don't know how this will work on other operating systems other than Windows but I would appreciate it if someone can do a test with this through an authenticated proxy connection.

Now for the code:

package javadownload;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

interface DowloadEvent
{
int getUpdateInterval();
void update(int received, int total);
}

class Report implements DowloadEvent
{
public int getUpdateInterval()
{
return 1000;
}

public void update(int received, int total)
{
System.out.println((int)(received / (float)total * 100) + " % completed");
}
}

class DownloadHandler
{
public static URLConnection getConnection(String url) throws MalformedURLException, IOException
{
return new URL(url).openConnection();
}

public static void downloadData(URLConnection from, OutputStream output, DowloadEvent downloadEvent) throws IOException
{
InputStream input = from.getInputStream();
byte[] data = new byte[1024];
long time;
int received = 0;
int rec;
int total = from.getContentLength();

time = System.currentTimeMillis();

while((rec = input.read(data)) > -1)
{
output.write(data, 0, rec);
received += rec;

if(System.currentTimeMillis() - time >= downloadEvent.getUpdateInterval())
{
time = System.currentTimeMillis();
downloadEvent.update(received, total);
}
}

input.close();
}
}

public class Main
{
private static String getFilenameFromUrl(String url)
{
int index = url.lastIndexOf("/");
return url.substring(index + 1);
}

public static void main(String[] args)
{
PrintStream p = System.out;

if(args.length != 1)
{
p.println("Specify the url to download from.");
return;
}

//Use the system's proxy settings specified in the Java control panel.
System.setProperty("java.net.useSystemProxies", "true");

String url = args[0];
String filename = getFilenameFromUrl(url);
URLConnection con;
FileOutputStream fout;
int length;

try
{
p.println("Connecting to: " + url);

con = DownloadHandler.getConnection(url);
con.setUseCaches(false);

length = con.getContentLength();

fout = new FileOutputStream(filename);

p.println("File size: " + (int)(length / 1024.0f) + " Kbytes");
p.println("Downloading...");

DownloadHandler.downloadData(con, fout, new Report());

p.println("Done.");
fout.close();
}
catch (MalformedURLException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}


No comments: