package com.krickert.wget; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; /** * simple wget - takes a file and saves it to disk. * * @author krickert * */ public class Wget { /** * Performs the wget. This is a simple static function that was made to * simplfy the way to just download a file from the internet to make it as * easy as a browser * * * @param saveAsFile * the file to save as from the internets * @param urlOfFile * the URL of the file to download * @return the status if it works */ public static WgetStatus wGet(String saveAsFile, String urlOfFile) { InputStream httpIn = null; OutputStream fileOutput = null; OutputStream bufferedOut = null; try { // check the http connection before we do anything to the fs httpIn = new BufferedInputStream(new URL(urlOfFile).openStream()); // prep saving the file fileOutput = new FileOutputStream(saveAsFile); bufferedOut = new BufferedOutputStream(fileOutput, 1024); byte data[] = new byte[1024]; boolean fileComplete = false; int count = 0; while (!fileComplete) { count = httpIn.read(data, 0, 1024); if (count <= 0) { fileComplete = true; } else { bufferedOut.write(data, 0, count); } } } catch (MalformedURLException e) { return WgetStatus.MalformedUrl; } catch (IOException e) { return WgetStatus.IoException; } finally { try { bufferedOut.close(); fileOutput.close(); httpIn.close(); } catch (IOException e) { return WgetStatus.UnableToCloseOutputStream; } } return WgetStatus.Success; } }