Here I am trying to download multiple files one after another:
Environment - Java 1.6
public List<Attachment> download(List<Attachment> attachments)
{
for(Attachment attachment : attachments) {
attachment.setDownStatus("Failed");
String destLocation = "C:\Users\attachments";
try {
String attUrl = attachment.getUrl();
String fileName = attachment.getFileName();
URL url = new URL(attUrl);
File fileLocation = new File(destLoc, fileName);
FileUtils.copyURLToFile(url, fileLocation);
if(fileLocation.exists()) {
attachment.setDownStatus("Completed");
}
} catch(Exception e) {
attachment.setDownStatus("Failed");
} finally {
attachment.setDestLocation(destLocation);
}
}
return attachments;
}
I am downloading the file from provided URL (http://cdn.octafinance.com/wp-content/uploads/2015/07/google-hummingbird.jpg).
FileUtils.copyURLToFile(url, fileLocation);
The above code does its downloading job perfectly, without any issues.
My Problem:
If the list of attachments are more it will take more time, so I would like to make it an asynchronous or parallel process instead of downloading sequentially.