ImageDownloader.java
package ac.essex.ooechs.imaging.commons.apps.multiple;
import ac.essex.ooechs.imaging.commons.PixelLoader;
import ac.essex.ooechs.imaging.commons.Resizer;
import ac.essex.ooechs.imaging.commons.ImageWindow;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Date;
class ImageDownloader extends Thread {
public static final File SAVE_TO = new File("/home/ooechs/Desktop/building");
String httpURL;
String filename;
public static void main(String[] args) {
try {
DecimalFormat f = new DecimalFormat("000");
String prefix = "18feb";
int count = 0;
for (int i = 0; i < 100; i++) {
new ImageDownloader("http://emcam1nd/record/current.jpg", prefix + "_" + f.format(count) + ".jpg").start();
Thread.sleep(500);
count++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public ImageDownloader(String httpURL, String filename) {
this.httpURL = httpURL;
this.filename = filename;
}
public void run() {
long start = System.currentTimeMillis();
try {
// ensure the save directory exists, otherwise create it.
File directory = SAVE_TO;
if (!directory.exists()) {
System.out.println("Creating directory: " + SAVE_TO);
directory.mkdirs();
}
String Imagefilename = httpURL.substring(httpURL.lastIndexOf('/') + 1).toLowerCase();
if (Imagefilename.endsWith("gif")) return;
System.out.println("Downloading: " + httpURL);
// create the url
URL url = new URL(httpURL);
InputStream in = url.openStream();
File file = new File(directory, filename);
FileOutputStream out = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.close();
System.out.println("Saved: " + filename);
PixelLoader image = new PixelLoader(file);
if (!image.loadedOK) {
file.delete();
}
} catch (FileNotFoundException fe) {
System.err.println("File Not Found: " + httpURL);
} catch (Exception e) {
System.err.println("Exception caught: " + e.getMessage());
} finally {
}
System.out.println("Time taken: " + (System.currentTimeMillis() - start));
}
}