0

I have some code that downloads the latest version of a jar file from a website. I use mydomain.com for my web server and the domain. I've had problems with html and php files not updating for hours using this service before. This time, I'm pretty sure it's Java that's caching the file or something, because if I click the download button on the site structure menu, it downloads the most recent version of the jar file. But when I do it in Java, it downloads a version that it got a while ago. This is the code I use to download the jar file:

System.out.println("starting update...");

        File rootDir = new File(System.getProperty("user.home") + "\\AppData\\Local\\GDRQ");
        File downloadDir = new File(System.getProperty("user.home") + "\\AppData\\Local\\GDRQ\\downloading");
        File targetFile = new File(System.getProperty("user.home") + "\\AppData\\Local\\GDRQ\\downloading\\launch_gdbotbin.jar");
        URL downloadUrl = new URL("http://dropmoose.com/gdbotpro/launch_gdbotbin.jar");

        if (!rootDir.isDirectory()) {
            rootDir.mkdir();
        }

        if (!downloadDir.isDirectory()) {
            downloadDir.mkdir();
        }

        InputStream inStream = downloadUrl.openStream();
        //System.out.println(inStream.available());
        BufferedInputStream bufIn = new BufferedInputStream(inStream);
        OutputStream out = new FileOutputStream(targetFile);
        BufferedOutputStream bufOut = new BufferedOutputStream(out);
        byte[] buffer = new byte[32 * 4096];

        while (true) {
            int nRead = bufIn.read(buffer, 0, buffer.length);

            if (nRead <= 0) {
                break;
            }

            bufOut.write(buffer, 0, nRead);
        }

        bufOut.flush();
        out.close();
        inStream.close();

        System.out.println("update successful");
        infoPane.dispose();

        System.out.println("cleaning up...");

UPDATE 1 hour later, I ran the download program again and it downloaded the latest version. But why does it take so long?

  • Perhaps you have something along the way that's caching responses. See http://stackoverflow.com/questions/17769239/java-how-can-i-download-a-non-cached-version-of-a-file for more about what you can do to resolve this. – childofsoong Apr 27 '16 at 23:12
  • I tried appending "?t=current time in milliseconds" to the url and it worked. Thank you very much! – TreeHouseFalcon Apr 27 '16 at 23:33

0 Answers0