fun2oos
Posts: 4
|
Posted: 01/29/2006, 10:23 PM |
|
//------------------------------------------------------------//
// DownloadURL.java: //
//------------------------------------------------------------//
// A Java program that demonstrates a procedure that can be //
// used to download the contents of a specified URL //
// (Image in this case). Takes File name (containg URL list) as input. //
//------------------------------------------------------------//
import java.io.*;
import java.net.*;
import java.io.File;
import java.util.*;
public class DownloadURL {
//-----------------------------------------------------//
// Execute: java DownloadURL UrlListFile.txt //
//-----------------------------------------------------//
public static void main (String[] args)
{
DownloadURL dnlUrl = new DownloadURL();
String fileName = "";
try
{
fileName = args[0];
}
catch(Exception e)
{
System.out.println("Url File name not specified : " + e);
System.exit(0);
}
long startTime=System.currentTimeMillis();
dnlUrl.GetUrlContents(fileName);
long endTime=System.currentTimeMillis();
float execTime = (((float)(endTime - startTime) / 1000) / 60);
System.out.println("\n\nTime taken for Execution = " + execTime + " min\n\t\tOR\nTime taken for Execution = " + (execTime / 60) + " hr");
} // end of main
public static void GetUrlContents(String fileName)
{
//-----------------------------------------------------//
// Start creating a few objects needed. //
//-----------------------------------------------------//
URL url;
InputStream is = null;
String strOutFileName = "", strUrl = "";
FileOutputStream out = null; // declare a file output object
PrintStream p = null; // declare a print stream object
try
{
int urlCounter = 0;
BufferedReader in = new BufferedReader(new FileReader(fileName));
while((strUrl = in.readLine()) != null)
{
try
{
url = new URL(strUrl);
is = url.openStream(); // throws an IOException
try
{
strOutFileName = strUrl.substring(strUrl.lastIndexOf("/") + 1);
System.out.print("\n" + ++urlCounter + " : " + strOutFileName);
out = new FileOutputStream("./images/" + strOutFileName);
p = new PrintStream( out );
}
catch(Exception e)
{
System.out.println("Error in file handling (create) : " + e);
}
BufferedInputStream bis=new BufferedInputStream(is);
BufferedOutputStream bos=new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
System.out.print(" : Downloading");
while(-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
System.out.print(".");
bos.write(buff, 0, bytesRead);
bos.flush();
}
System.out.print("done");
bis.close();
bos.close();
}
catch (MalformedURLException mue)
{
System.out.println("Ouch - a MalformedURLException happened.");
mue.printStackTrace();
System.exit(1);
}
catch (IOException ioe)
{
System.out.println("Oops- an IOException happened.");
ioe.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
System.out.println("Exception happened : " + e);
e.printStackTrace();
System.exit(1);
}
finally
{
//---------------------------------//
// Close the InputStream //
//---------------------------------//
try
{
is.close();
}
catch (IOException ioe)
{
// just going to ignore this one
}
} // end of 'finally' clause
} // end of while
}
catch(Exception exp)
{
System.out.println("Error in File reading : " + exp);
}
}
} // end of class definition
If someone can tell me another method, it will b gr8.....
:-w
_________________
fun2oos |