自动下载资产并将其保存到目录

时间:2018-09-11 21:13:44

标签: java

我做了一个自动下载器,它将所有数据保存到user.home / OSR.cache 它使用Dropbox下载zip并将其解压缩到特定文件夹。 我的问题是,它尝试下载zip,但将其保存到0字节的目录中,再次检查了链接,它也开始下载,因此我不确定这里的问题是什么? 这是我的更新程序类

public class Updater {
    /**
     * The client version.
     */
    private static final String CLIENT_VERSION = "https://www.dropbox.com/s/j99anix5sngmktv/clientVersion.txt?dl=1";

    /**
     * The cache version.
     */
    private static final String CACHE_VERSION = "https://www.dropbox.com/s/6nfd34bantevv2i/cacheVersion.txt?dl=1";

    /**
     * The cache download url link.
     */
    public static final String CACHE_LINK = "https://www.dropbox.com/s/gsxglfqelnnnae4/TempCache5945.zip?dl=1";

    /**
     * The client download url link.
     */
    private static final String CLIENT_LINK = "https://www.dropbox.com/s/ctdiirkasrf1grh/Client.jar?dl=1";

    /**
     * The cache directory.
     */
    private static final File CACHE_DIRECTORY = new File(Utility.findcachedir());

    /**
     * The current update state.
     */
    public UpdateState state = UpdateState.CHECK_FOR_UPDATES;

    /**
     * An instance of updater
     */
    private static final Updater INSTANCE = new Updater();

    /**
     * Constructs a new {@link Updater}.
     */
    private Updater() {
        /* can't initialize this class. */
    }

    public void setup(Client client) {
        client.updaterRenderer = new UpdaterRenderer(client, client.graphics);
        if (Client.virgin) {
            state = UpdateState.WELCOME_SCREEN;
            client.updaterRenderer.setScreen(new WelcomeUpdateScreen());
        } else {
            state = UpdateState.DEFAULT_SCREEN;
            client.updaterRenderer.setScreen(new DefaultUpdateScreen());
        }
    }

    public void checkForUpdates() {
        Client client = Client.instance;
        if (checkClientUpdates()) {
            UpdateComponent screen = new ClientUpdateScreen();
            client.updaterRenderer.setScreen(screen);

            try {
                final String path = getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
                updateClient(path.substring(path.lastIndexOf("/") + 1, path.indexOf(".jar")), screen);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        } else if (checkCacheUpdate()) {
            UpdateComponent screen = new CacheUpdateScreen();
            client.updaterRenderer.setScreen(screen);
            try {
                updateCache(screen);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        finish();
    }

    public void finish() {
        if (Client.virgin) {
            UpdateComponent screen = new GoodbyeUpdateScreen();
            Client.instance.updaterRenderer.setScreen(screen);
            return;
        }

        if (state != UpdateState.FINISHED) {
            Client client = Client.instance;
            Client.updaterScreenIP = null;
            client.updaterRenderer.finish();
            client.updaterRenderer = null;
            System.gc();
            System.runFinalization();
            client.startUp();
            state = UpdateState.FINISHED;
        }
    }

    public static boolean isActive() {
        return get().state != UpdateState.FINISHED;
    }

    /**
     * Checks the web server for client jar updates.
     */
    private boolean checkClientUpdates() {
        if (Configuration.DEBUG_MODE) {
            return false;
        }

        int clientVersion = Integer.parseInt(Utility.getNewestVersion(CLIENT_VERSION));
        int current = Configuration.CLIENT_VERSION;

        System.out.println(clientVersion + " " + current);

        // Incorrect client version
        if (current != clientVersion) {
            System.out.println("Client update required!");
            state = UpdateState.UPDATE_CLIENT;
        }
        return state == UpdateState.UPDATE_CLIENT;
    }

    /**
     * Checks the web server for cache updates.
     */
    private boolean checkCacheUpdate() {
        if (Configuration.DEBUG_MODE) {
            return false;
        }

        File dat = new File(Utility.findcachedir() + Configuration.SPRITE_FILE_NAME + ".dat");
        File idx = new File(Utility.findcachedir() + Configuration.SPRITE_FILE_NAME + ".idx");

        // do the sprite files exist?
        if (!dat.exists() || !idx.exists()) {
            System.out.println("Cache sprite files do not exist!");
            state = UpdateState.UPDATE_CACHE;
            return true;
        }

        File versionFile = new File(Utility.findcachedir() + "version.dat");

        // check if the version file exists
        if (!versionFile.exists()) {
            System.out.println("Version file does not exist!");
            state = UpdateState.UPDATE_CACHE;
            return true;
        }

        String version = Utility.getNewestVersion(CACHE_VERSION);
        String current = Utility.getCurrentVersion(Utility.findcachedir() + "version.dat");

        // does the version of the cache match the updated version
        if (!current.equalsIgnoreCase(version)) {
            System.out.println("Version file mismatch!");
            state = UpdateState.UPDATE_CACHE;
            return true;
        }

        return state == UpdateState.UPDATE_CACHE;
    }

    /**
     * Updates the current client jar file with the new downloaded one.
     *
     * @param jarName the name of this jar file
     */
    private void updateClient(String jarName, UpdateComponent screen) {
        File client = new File(System.getProperty("user.dir") + File.separator + jarName + ".jar");
        try {
            File temp = File.createTempFile("tmp", Long.toString(System.nanoTime()));
            temp.deleteOnExit();
            download(CLIENT_LINK, temp, screen);
            replaceData(temp, client);
            Runtime.getRuntime().exec("java -jar " + jarName + ".jar");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

    public void updateCache(UpdateComponent screen) throws IOException {
        File temp = File.createTempFile("tmp", Long.toString(System.nanoTime()));
        temp.deleteOnExit();
        download(CACHE_LINK, temp, screen);
        unZipFile(temp, CACHE_DIRECTORY, (CacheUpdateScreen) screen);
        try {
            File file = new File(Utility.findcachedir() + File.separator + "version.dat");
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write("" + Configuration.CACHE_VERSION);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        state = UpdateState.UP_TO_DATE;
        Client.instance.updaterRenderer.setScreen(new UpToDateUpdateScreen());
        System.out.println("Cache was downloaded and extracted successfully.");
    }

    private void download(String link, File target, UpdateComponent screen) {
        try {
            URL url = new URL(link);
            URLConnection conn = url.openConnection();
            BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
            FileOutputStream out = new FileOutputStream(target);
            if (state == UpdateState.UPDATE_CLIENT) {
                screen.setLabel("Downloading client files");
            } else if (state == UpdateState.UPDATE_CACHE) {
                screen.setLabel("Downloading cache files");
            }
            int length;
            long downloaded = 0;
            int total = conn.getContentLength();
            byte[] bytes = new byte[1024];
            while ((length = in.read(bytes)) != -1) {
                out.write(bytes, 0, length);
                downloaded += length;
                if (state == UpdateState.UPDATE_CLIENT) {
                    ((ClientUpdateScreen) screen).setProgress((int) ((downloaded / (double) total * 100)));
                } else if (state == UpdateState.UPDATE_CACHE) {
                    ((CacheUpdateScreen) screen).setProgress((int) ((downloaded / (double) total * 100)));
                }
            }
            in.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void unZipFile(File zip, File directory, CacheUpdateScreen screen) throws IOException {
        screen.setLabel("Extracting cache files");
        screen.setProgress(0);

        byte[] bytes = new byte[1024];

        ZipEntry entry;
        ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(zip)));

        while ((entry = in.getNextEntry()) != null) {
            File file = new File(directory, entry.getName());
            if (entry.isDirectory()) {
                if (!file.mkdirs()) {
                    System.err.println("Could not create directory '" + file.toPath() + "'!");
                }
            } else {
                FileOutputStream out = new FileOutputStream(file);
                int len;
                long unzipped = 0;
                long total = entry.getSize();
                while ((len = in.read(bytes)) > -1) {
                    out.write(bytes, 0, len);
                    unzipped += len;
                    screen.setProgress((int) (unzipped / (double) total * 100));
                }
                out.flush();
                out.close();
            }
        }
        in.close();
    }

    /**
     * Replaces the contents of the source file into the target file.
     *
     * @param source the source file
     * @param target the target file
     * @throws IOException if there was an exception replacing the files
     */
    private static void replaceData(File source, File target) throws IOException {
        ZipEntry entry;
        int length;
        byte[] buffer = new byte[1024];
        try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(source))); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)))) {
            while ((entry = in.getNextEntry()) != null) {
                if (entry.isDirectory())
                    continue;

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                while ((length = in.read(buffer, 0, buffer.length)) > -1) {
                    bytes.write(buffer, 0, length);
                }

                out.putNextEntry(entry);
                out.write(bytes.toByteArray());
                out.closeEntry();
            }
            out.finish();
        }
    }

    public static Updater get() {
        return INSTANCE;
    }
}

还有我的Utility类findcachedir方法

public static String findcachedir() {
            File file = new File(System.getProperty("user.home") + "/OSR.Cache/");
            if (!file.exists()) {
                if (!file.mkdir()) {
                    return secondDir();
                }
                Client.virgin = true;
            }
            return System.getProperty("user.home") + "/OSR.Cache/";
        }


    public static String secondDir() {
        File file = new File("c:/OSR.Cache/");
        if (!file.exists() && file.mkdir())
            Client.virgin = true;
        return file.toString();
    }

有人能启发我为什么它没有被保存吗?

0 个答案:

没有答案