|
|
|
@ -13,8 +13,12 @@ import ucar.nc2.NetcdfFiles;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.ConnectException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.channels.Channels;
|
|
|
|
|
import java.nio.channels.ReadableByteChannel;
|
|
|
|
|
import java.time.*;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.*;
|
|
|
|
@ -62,37 +66,40 @@ public class GfsDownloader extends BaseDataDownloader {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean download(FileInfo fileInfo) {
|
|
|
|
|
try {
|
|
|
|
|
public FileInfo download(FileInfo fileInfo) {
|
|
|
|
|
return download0(fileInfo, 2);
|
|
|
|
|
}
|
|
|
|
|
private FileInfo download0(FileInfo fileInfo, int retryNum) {
|
|
|
|
|
|
|
|
|
|
String url = fileInfo.getUrl();
|
|
|
|
|
File destDir = new File(fileInfo.getSavePath());
|
|
|
|
|
if (!destDir.exists()) {
|
|
|
|
|
destDir.mkdirs();
|
|
|
|
|
if (!destDir.exists() && !destDir.mkdirs()) {
|
|
|
|
|
throw new RuntimeException("创建文件夹" + destDir + "失败");
|
|
|
|
|
}
|
|
|
|
|
File fileOut = new File(destDir, fileInfo.getFileName());
|
|
|
|
|
// if (fileOut.exists()) {
|
|
|
|
|
// log.info("[GFS Download] 文件已存在,忽略下载: {}", fileOut.getAbsolutePath());
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
log.info("[GFS Download] 文件下载中,保存至 {}", fileOut);
|
|
|
|
|
// FileOutputStream fos = new FileOutputStream(filePath);
|
|
|
|
|
// try {
|
|
|
|
|
// ReadableByteChannel rbc = Channels.newChannel(new URL(url).openStream());
|
|
|
|
|
// fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
|
|
|
|
// } catch (ConnectException e) {
|
|
|
|
|
// log.error("连接超时:{}", url);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// TODO 2024/5/24: 增加重试机制
|
|
|
|
|
// if (fileInfo.getForecastHour() == 12 || fileInfo.getForecastHour() == 18 || fileInfo.getForecastHour() == 24) {
|
|
|
|
|
// throw new RuntimeException();
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// fos.flush();
|
|
|
|
|
// fos.close();
|
|
|
|
|
FileUtils.copyURLToFile(new URL(url), fileOut);
|
|
|
|
|
FileUtils.copyURLToFile(new URL(url), fileOut, 30000, 30000);
|
|
|
|
|
log.info("[GFS Download] 文件下载成功: {}", fileOut);
|
|
|
|
|
return fileValid(fileOut.getAbsolutePath());
|
|
|
|
|
fileInfo.setDownloadSuccess(fileValid(fileOut.getAbsolutePath()));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("[GFS Download]下载失败:{} {}", e.getMessage(), fileInfo);
|
|
|
|
|
return false;
|
|
|
|
|
fileInfo.setDownloadSuccess(false);
|
|
|
|
|
if (retryNum > 0) {
|
|
|
|
|
log.error("[GFS Download] 文件下载失败,重试中: {}", fileOut);
|
|
|
|
|
return download0(fileInfo, retryNum - 1);
|
|
|
|
|
} else {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
log.error("[GFS Download] 文件下载失败: {}", fileOut);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fileInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean fileValid(String file) {
|
|
|
|
|
try (NetcdfFile ncFile = NetcdfFiles.open(file)) {
|
|
|
|
|
ncFile.getLocation();
|
|
|
|
@ -102,22 +109,22 @@ public class GfsDownloader extends BaseDataDownloader {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public boolean downloadAll(List<FileInfo> fileInfoList) {
|
|
|
|
|
public List<FileInfo> downloadAll(List<FileInfo> fileInfoList) {
|
|
|
|
|
log.info("[GFS Download] 下载任务启动,共 {} 个文件", fileInfoList.size());
|
|
|
|
|
List<Future<Boolean>> futures = new ArrayList<>();
|
|
|
|
|
List<Future<FileInfo>> futures = new ArrayList<>();
|
|
|
|
|
List<FileInfo> finishList = new ArrayList<>();
|
|
|
|
|
for (FileInfo fileInfo : fileInfoList) {
|
|
|
|
|
futures.add(executorService.submit(() -> download(fileInfo)));
|
|
|
|
|
}
|
|
|
|
|
boolean allCompleted = true;
|
|
|
|
|
for (Future<Boolean> future : futures) {
|
|
|
|
|
for (Future<FileInfo> future : futures) {
|
|
|
|
|
try {
|
|
|
|
|
allCompleted = allCompleted && future.get();
|
|
|
|
|
finishList.add(future.get());
|
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.info("[GFS Download] 下载完成,共 {} 个文件", fileInfoList.size());
|
|
|
|
|
return allCompleted;
|
|
|
|
|
log.info("[GFS Download] 下载完成,共 {} 个文件", finishList.size());
|
|
|
|
|
return finishList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|