Java.Wget

Downloading files from web with Java

/*
 * This is a simply wget.
 * You can test your URL
 *
 * $Log$
 */
 
import java.io.IOException;
import java.io.EOFException;
import java.io.DataInputStream;
import java.io.PrintStream;
 
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
 
/**
 * @version $Revision$
 */
public class wget {
  static final PrintStream out = System.out;
  static final String commandName = wget.class.getName();
  static int count;
  static boolean verb;
  static boolean output = true;
 
  public static void main(String args[]) throws IOException {
    try {
      while (args[0].charAt(0) == '-') {
        String argument = args[0].substring(1);
        if (argument.equals("v")) {
          verb = true;
        } else if (argument.equals("np")) {
          output = false;
        }
        args[0] = args[1];
        if (args.length > 2) {
          args[1] = args[2];
        }
      }
    } catch (ArrayIndexOutOfBoundsException e) {
      out.println( "USAGE: " + commandName + " [-v] [-np] <url>" );
      out.println( "\t-v  : verbose all action." );
      out.println( "\t-np : don't print the data from the URL." );
      System.exit( 1 );
    }
 
    URLConnection url = (new URL(args[0])).openConnection();
 
    if (url instanceof HttpURLConnection) {
      readHttpURL((HttpURLConnection) url);
    } else {
      readURL(url);
    }
  }
 
  public static final void readURL(URLConnection url) throws IOException {
    DataInputStream in = new DataInputStream(url.getInputStream());
    printHeader(url);
 
    try {
      while (true) {
        writeChar((char) in.readUnsignedByte());
      }
    } catch (EOFException e) {
      if (output) verbose("\n");
      verbose(commandName +
              ": Read " + count +
              " bytes from " + url.getURL());
    } catch (IOException e) {
      out.println( e + ": " + e.getMessage());
      if (output) verbose("\n");
      verbose(commandName +
              ": Read " + count +
              " bytes from " + url.getURL());
    }
    System.exit(0);
  }
 
  public static final void readHttpURL(HttpURLConnection url) 
    throws IOException {
 
    long before, after;
 
    url.setAllowUserInteraction (true);
    verbose(commandName + ": Contacting the URL ...");
    url.connect();
    verbose(commandName + ": Connect. Waiting for reply ...");
    before = System.currentTimeMillis();
    DataInputStream in = new DataInputStream(url.getInputStream());
    after = System.currentTimeMillis();
    verbose(commandName + ": The reply takes " + 
            ((int) (after - before) / 1000) + " seconds");
 
    before = System.currentTimeMillis();
 
 
    try {
      if (url.getResponseCode() != HttpURLConnection.HTTP_OK) {
        out.println(commandName + ": " + url.getResponseMessage());
      } else {
        printHeader(url);
        while (true) {
          writeChar((char) in.readUnsignedByte());
        }
      }
    } catch (EOFException e) {
      after = System.currentTimeMillis();
      int milliSeconds = (int) (after-before);
      if (output) verbose("\n");
      verbose(commandName +
              ": Read " + count +
              " bytes from " + url.getURL());
      verbose(commandName + ": HTTP/1.0 " + url.getResponseCode() +
              " " + url.getResponseMessage());
      url.disconnect();
 
      verbose(commandName + ": It takes " + (milliSeconds/1000) + 
              " seconds" + " (at " + round(count/(float) milliSeconds) + 
              " K/sec).");
      if (url.usingProxy()) {
        verbose(commandName + ": This URL uses a proxy");
      }
    } catch (IOException e) {
      out.println( e + ": " + e.getMessage());
      if (output) verbose("\n");
      verbose(commandName +
              ": I/O Error : Read " + count +
              " bytes from " + url.getURL());
      out.println(commandName + ": I/O Error " + url.getResponseMessage());
    }
    System.exit(0);
  }
 
  public static final void printHeader(URLConnection url) {
    verbose(wget.class.getName() + ": Content-Length   : " + 
            url.getContentLength() );
    verbose(wget.class.getName() + ": Content-Type     : " + 
            url.getContentType() );
    if (url.getContentEncoding() != null)
      verbose(wget.class.getName() + ": Content-Encoding : " + 
              url.getContentEncoding() );
    if (output) verbose("");
  }
 
  public final static void writeChar(char c) {
    if (output) out.print(c);
    count++;
  }
 
  public static final void verbose(String s) {
    if (verb) out.println( s );
  }
 
  public static final float round(float f) {
    return Math.round(f * 100) / (float) 100;
  }
}

http://koala.ilog.fr/plh/generator/wget.html

import java.io.*;
import java.net.*;
import java.util.Vector;
 
/** This class does a simple HTTP GET and writes the retrieved content to a local file
 * 
 * @author Brian Pipa - http://pipasoft.com
 * @version 1.0
 */
public class wget {
 
    static final String FS = File.separator;
 
    /** This method does the actual GET
     * 
     * @param theUrl The URL to retrieve
     * @param filename the local file to save to
     * @exception IOException 
     */
    public void get(String theUrl, String filename) throws IOException
    {
        try {
            URL gotoUrl = new URL(theUrl);
            InputStreamReader isr = new InputStreamReader(gotoUrl.openStream());
            BufferedReader in = new BufferedReader(isr);
 
            StringBuffer sb = new StringBuffer();
            String inputLine;
            boolean isFirst = true;
 
            //grab the contents at the URL
            while ((inputLine = in.readLine()) != null){
                sb.append(inputLine+"\r\n");
            }
            //write it locally
            createAFile(filename, sb.toString());
        }
        catch (MalformedURLException mue) {
            mue.printStackTrace();
        }
        catch (IOException ioe) {
            throw ioe;
        }
    }
 
    //creates a local file
    /** Writes a String to a local file
     * 
     * @param outfile the file to write to
     * @param content the contents of the file
     * @exception IOException 
     */
    public static void createAFile(String outfile, String content) throws IOException {
        FileOutputStream fileoutputstream = new FileOutputStream(outfile);
        DataOutputStream dataoutputstream = new DataOutputStream(fileoutputstream);
        dataoutputstream.writeBytes(content);
        dataoutputstream.flush();
        dataoutputstream.close();
    }
 
    /** The main method.
     * 
     * @param args 
     */
    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("\nUsage: java wget URL localfilename");
            System.out.println("Example: java wget http://google.com google.html");
            System.exit(1);
        }
        try {
            wget httpGetter = new wget();
            httpGetter.get(args[0], args[1]);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
 
    }
}

http://pipasoft.com/free-java-classes/download/wget.java

import java.io.*;
import java.net.*;
 
/*
 * Command line program to download data from URLs and save
 * it to local files. Run like this:
 * java FileDownload http://schmidt.devlib.org/java/file-download.html
 * @author Marco Schmidt
 */
public class FileDownload {
	public static void download(String address, String localFileName) {
		OutputStream out = null;
		URLConnection conn = null;
		InputStream  in = null;
		try {
			URL url = new URL(address);
			out = new BufferedOutputStream(
				new FileOutputStream(localFileName));
			conn = url.openConnection();
			in = conn.getInputStream();
			byte[] buffer = new byte[1024];
			int numRead;
			long numWritten = 0;
			while ((numRead = in.read(buffer)) != -1) {
				out.write(buffer, 0, numRead);
				numWritten += numRead;
			}
			System.out.println(localFileName + "\t" + numWritten);
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException ioe) {
			}
		}
	}
 
	public static void download(String address) {
		int lastSlashIndex = address.lastIndexOf('/');
		if (lastSlashIndex >= 0 &&
		    lastSlashIndex < address.length() - 1) {
			download(address, address.substring(lastSlashIndex + 1));
		} else {
			System.err.println("Could not figure out local file name for " +
				address);
		}
	}
 
	public static void main(String[] args) {
		for (int i = 0; i < args.length; i++) {
			download(args[i]);
		}
	}
}

http://schmidt.devlib.org/java/file-download.html

 
coding/java/wget.txt · Last modified: 2008/05/12 09:32 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki