创建更新程序.更新/下载部件

Ruu*_*ddR 3 java download updates

我想创建一个更新程序,但我不确定我是如何做这个部分的.我希望我的更新程序检查更新,然后计算它必须执行的所有更新,如下所示:您仍需要3/5更新.或类似的东西.我知道它检查是否有可用的更新,如果有更新,那么它会直接下载该更新,然后检查另一个更新并下载,但我希望它检查所有更新,然后下载它们.这是Launcher类

package com;

import com.check.Checker;
import com.download.DownloadData.DownloadFiles;

public class Launcher {

    public static void main(final String[] args) {
        for (DownloadFiles df: DownloadFiles.values()) {

            String fileName = df.fileName;
            String URL = df.URL;

            switch(Checker.isLatest(fileName, URL)) {
            case 0:
                System.out.println("Downloading " + fileName + ".");
                break;
            case 1:
                System.out.println("Updating " + fileName + ".");
                break;
            case 2:
                System.out.println(fileName + " is up to date.");
                break;
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是isLatest方法:

public static byte isLatest(String fileName, String downloadUrl) {
        if (!hasFile(fileName)) {
            System.out.println("[" + fileName + "]" + "There is no file yet.");
            return 0;
        }
        try {
            if (DigestUtils.md5Hex(new FileInputStream(Settings.saveDir)).equalsIgnoreCase(DigestUtils.md5Hex(new URL(downloadUrl).openStream()))) {
                    System.out.println("[" + fileName + "]" + "Your file is up to date.");
                return 2;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("[" + fileName + "]" + "Your file needs to be updated.");
        return 1;
    }
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:如何使启动器按此顺序执行:检查更新.列出更新,然后下载更新.

k0n*_*ner 5

我不完全确定我理解你的问题,但也许你想做某事:

//in Launcher

EnumSet<DownloadFileS> filesToUpdate = checkForUpdates(DownloadFiles.values());

listFilesToUpdate(filesToUpdate);

downloadFiles(filesToUpdate);
Run Code Online (Sandbox Code Playgroud)

然后,checkForUpdates您只需创建自上次同步后已更改的文件列表,listFilesToUpdate只需将其打印出来并downloadFiles(filesToUpdate)下载即可.基本上你已经拥有了isLatest方法中的所有东西- 你只需要按责任分割它.

UPDATE

public List<DownloadFiles> checkForUpdates(List<DownloadFiles> allFiles) {
    allFiles.stream()
        .filter(file -> !hasFile(file.getFilename()) || !fileUpToDate(file))
        .collect(toList())

private boolean isFileUpToDate(DownloadFiles file) {
    return DigestUtils.md5Hex(new FileInputStream(Settings.saveDir))
       .equalsIgnoreCase(DigestUtils.md5Hex(new URL(downloadUrl).openStream())));
Run Code Online (Sandbox Code Playgroud)

下一步更新

public static void main(String[] args) {
  List<DownloadFiles> filesToUpdate = checkForUpdates(DownloadFiles.values());

  listFilesToUpdate(filesToUpdate);

  downloadFiles(filesToUpdate);
}

private static void downloadFiles(List<DownloadFiles> filesToUpdate) {
  filesToUpdate.stream()
      .forEach(file -> downloadFile(file));
}

private static void downloadFile(DownloadFiles file) {
  try {
    URL website = new URL(file.URL);
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(file.filename);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

private static void listFilesToUpdate(List<DownloadFiles> filesToUpdate) {
  System.out.println("Following files will be updated");
  filesToUpdate.stream()
      .forEach(System.out::println);
}

public static List<DownloadFiles> checkForUpdates(DownloadFiles[] allFiles) {
  return Arrays.asList(allFiles).stream()
      .filter(file -> !hasFile(file.filename) || !isFileUpToDate(file))
      .collect(Collectors.toList());
}

private static boolean isFileUpToDate(DownloadFiles file) {
  try (InputStream is = new URL(file.URL).openStream()) {
    return DigestUtils.md5Hex(new FileInputStream(Settings.saveDir))
        .equalsIgnoreCase(DigestUtils.md5Hex(is));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
Run Code Online (Sandbox Code Playgroud)