Skip to content

Instantly share code, notes, and snippets.

@oleg
Created March 14, 2013 08:29
Show Gist options
  • Save oleg/5159776 to your computer and use it in GitHub Desktop.
Save oleg/5159776 to your computer and use it in GitHub Desktop.
Simple download automatisation
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) throws IOException {
final ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 5001; i++) {
executorService.submit(new DownloadAndSave(i));
}
}
}
class DownloadAndSave implements Runnable {
private final int num;
DownloadAndSave(int num) {
this.num = num;
}
@Override
public void run() {
try {
Files.copy(
new URL(url()).openConnection().getInputStream(),
Paths.get(filename()));
System.out.println(num);
} catch (IOException e) {
System.out.println(num + " " + e.getMessage());
}
}
String filename() {
return "todo" + num;
}
String url() {
return "http://example.org/?id=" + num;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment