实际上,经过仔细研究,鲍里斯的代码是错误的,有时确实不会设置一些东西.这是一个更好的版本,修复了:
public List download(List attachments) {
ExecutorService executorService = Executors.newCachedThreadPool();
List> futures = new ArrayList>();
for (final Attachment attachment : attachments) {
futures.add(executorService.submit(new Callable() {
@Override
public Attachment call() throws Exception {
return doDownload(attachment);
}
}));
}
for (Future 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;
}
但是,鉴于您的附件结构以及如何使用它,这绝对不是最佳选择.我没有解决这个问题:我只回答了问题.