小编Upp*_*rla的帖子

在Java中并行或异步下载多个文件

在这里,我试图一个接一个地下载多个文件:

环境-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)

上面的代码完美地完成了下载工作,没有任何问题。

我的问题:
如果附件列表更多,则将花费更多时间,因此我想使其成为异步或并行过程,而不是顺序下载。

java multithreading asynchronous file java-6

4
推荐指数
2
解决办法
3250
查看次数

解析MMMM YYYY格式的日期

我必须将日期字符串(例如"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)

但由于缺少这一天,它不起作用.

java format date

0
推荐指数
1
解决办法
2062
查看次数

标签 统计

java ×2

asynchronous ×1

date ×1

file ×1

format ×1

java-6 ×1

multithreading ×1