import java.net.*; import java.io.*; import java.lang.*; import java.util.*; public class getURL { //retrieves the url and returns as a string the // name of the local file stored public static String geturl(String url,String outname) { String outfile=""; try { System.out.println("Getting URL "+url); if (outname.equals("")) { if ( (url.substring(url.length()-1,url.length()).equals("/")) || (url.substring(url.length()-4,url.length()).equals(".com")) || (url.substring(url.length()-4,url.length()).equals(".net")) || (url.substring(url.length()-4,url.length()).equals(".org")) ) outfile="index.html"; else { int pos1=1; StringTokenizer st=new StringTokenizer(url,"/"); String word; while (st.hasMoreTokens()) { word=st.nextToken(); pos1=url.indexOf(word); } outfile=url.substring(pos1,url.length()); } } else outfile=outname; URL myurl=new URL(url); BufferedReader in = new BufferedReader(new InputStreamReader( myurl.openStream())); String inputLine; PrintWriter fw=new PrintWriter(new FileWriter(outfile)); while ((inputLine = in.readLine()) != null) fw.println(inputLine); in.close(); fw.close(); /* InputStream istream=myurl.openStream(); InputStreamReader isr=new InputStreamReader(istream); FileWriter fw=new FileWriter(outfile); while (isr.ready()) { fw.write(isr.read()); } isr.close(); fw.close(); */ } catch (Exception e) { e.printStackTrace(); } return outfile; } }