在Java中并行或异步下载多个文件 [英] Downloading Multiple Files Parallelly or Asynchronously in Java
问题描述
在这里,我试图一个接一个地下载多个文件:
Here I am trying to download multiple files one after another:
环境-Java 1.6
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;
}
我正在从提供的URL( http ://cdn.octafinance.com/wp-content/uploads/2015/07/google-hummingbird.jpg ).
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.
推荐答案
实际上,经过仔细查看,鲍里斯(Boris)的代码是有错误的,确实有时不会设置任何内容.这是一个更好的版本,可以解决此问题:
Actually, after carefully looking, Boris' code is faulty and will indeed not set some stuff sometimes. Here's a better version that fixes that:
public List<Attachment> download(List<Attachment> attachments) {
ExecutorService executorService = Executors.newCachedThreadPool();
List<Future<Attachment>> futures = new ArrayList<Future<Attachment>>();
for (final Attachment attachment : attachments) {
futures.add(executorService.submit(new Callable<Attachment>() {
@Override
public Attachment call() throws Exception {
return doDownload(attachment);
}
}));
}
for (Future<Attachment> future: futures) {
try {
future.get();
} catch (Exception ex) {
// Do something
}
}
return attachments;
}
private Attachment doDownload(Attachment attachment) throws Exception {
attachment.setDownStatus("Failed");
attachment.setDestLocation("C:\\Users\\attachments");
String attUrl = attachment.getUrl();
String fileName = attachment.getFileName();
URL url = new URL(attUrl);
File fileLocation = new File(attachment.getDestLocation(), fileName);
FileUtils.copyURLToFile(url, fileLocation);
if (fileLocation.exists()) {
attachment.setDownStatus("Completed");
}
return attachment;
}
但是,鉴于您的Attachment
结构及其使用方式,这绝对不是最佳选择.我没有解决这个问题:我只是回答了这个问题.
However, this is absolutely not optimal given your structure of Attachment
and how you use it. I did not fix that: I only answered the question as it was asked.
这篇关于在Java中并行或异步下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!