在这里,我试图一个接一个地下载多个文件:
环境-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;
}
Run Code Online (Sandbox Code Playgroud)
我正在从提供的URL(http://cdn.octafinance.com/wp-content/uploads/2015/07/google-hummingbird.jpg)下载文件。
FileUtils.copyURLToFile(url, fileLocation);
Run Code Online (Sandbox Code Playgroud)
上面的代码完美地完成了下载工作,没有任何问题。
我的问题:
如果附件列表更多,则将花费更多时间,因此我想使其成为异步或并行过程,而不是顺序下载。
我必须将日期字符串(例如"2015年10月")解析为日期.所以问题是:我如何解析MMMM yyyy格式的日期?如果新的Date对象是给定月份的第一个月,则可以.
我试过了:
DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("MMMM yyyy").toFormatter();
TemporalAccessor ta = formatter.parse(node.textValue());
Instant instant = LocalDate.from(ta).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date d = Date.from(instant);
Run Code Online (Sandbox Code Playgroud)
但由于缺少这一天,它不起作用.