I am writing a scraper that downloads a list of mp3s from a certain site (1,700+ mp3s of about 15 MB each). My method is given a List
of URLs and a destination folder.
I would like to program a JProgressBar
dialog around this that has:
- The current transfer rate.
- The currently-being-downloaded file information (how much is done / how much is left).
I am using a ReadableByteChannel
that transfers into a FileOutputStream
(or, more accurately, a FOS that transfers from the RBC).
Here's the code:
private static void download(List<String> mp3_urls, String directory) {
try {
for(String mp3_url : mp3_urls) {
URL url = new URL(mp3_url);
try (ReadableByteChannel rbc = Channels.newChannel(url.openStream());) {
Pattern pattern = Pattern.compile("[A-Za-z0-9_]*\\.mp3");
Matcher matcher = pattern.matcher(mp3_url);
matcher.find();
System.out.println("Reading: " + mp3_url);
try (FileOutputStream fos = new FileOutputStream(directory + File.separator + matcher.group())) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
My questions are:
- Does the
FileOutputStream::transferFrom
method throw any properties that could be caught with aPropertyChangeListener
that would give this information?? - Am I using methods capable of giving this information?
- How would you get this information?