Author Topic: [Java] URL Downloader  (Read 2345 times)

0 Members and 1 Guest are viewing this topic.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
[Java] URL Downloader
« on: October 01, 2007, 05:29:55 pm »
http://bnubot.googlecode.com/svn/trunk/BNUBot/src/net/bnubot/util/URLDownloader.java
Code: [Select]
/**
 * This file is distributed under the GPL
 * $Id: URLDownloader.java 716 2007-09-26 20:34:47Z scotta $
 */

package net.bnubot.util;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/**
 * @author sanderson
 *
 */
public class URLDownloader {
public static void downloadURL(URL url, File to, boolean force) throws IOException {
// Don't download the file if it already exists
if(to.exists() && (force == false))
return;

// Make sure the path to the file exists
{
String sep = System.getProperty("file.separator");
String folders = to.getPath();
String path = "";
for(int i = 0; i < folders.length(); i++) {
path += folders.charAt(i);
if(path.endsWith(sep)) {
File f = new File(path);
if(!f.exists())
f.mkdir();
if(!f.isDirectory()) {
Out.error(URLDownloader.class, path + " is not a directory!");
return;
}
}
}
}

Out.info(URLDownloader.class, "Downloading " + url.toExternalForm());

DataInputStream is = new DataInputStream(new BufferedInputStream(url.openStream()));
FileOutputStream os = new FileOutputStream(to);
byte[] b = new byte[0x1000];
do {
int c = is.read(b);
if(c == -1)
break;
os.write(b, 0, c);
} while(true);

os.close();
is.close();
}
}

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!