3

I have recently been trying to create an updater for my program. The updater is supposed to go to dropbox, look through the files in the 'public' folder, and decide if it exists. It works, and can DOWNLOAD the file, but it can't CHECK if the file exists. I saw this and I thought it was a solution, but it doesn't seem to be working.

Here is the code I'm using to check for the file's existence:

public static boolean exists(String URLName) {
    try {
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection con = (HttpURLConnection) new URL(URLName)
                .openConnection();
        con.setRequestMethod("HEAD");

        return (con.getResponseCode() == HttpURLConnection.HTTP_OK);

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

However, it seems to always return true. The files I'm accessing all begin with "App_" and end with ".zip". The only thing that varies is the version, which is in the #.### format.

Here is the FULL code for how I'm checking it:

    public static void main(String[] args) throws IOException,
            InterruptedException {

        double origVersion = 0.008;

        double versionTimes = 0.000;
        while(exists("http://dl.dropbox.com/u/.../" + "App_"+ String.valueOf(origVersion + versionTimes) + ".zip")) {
            versionTimes = round(versionTimes + 0.001);
            //origVersion = round(origVersion + 0.001);


            System.exit(0);

    }
    }
public static boolean exists(String URLName) {
        try {
            HttpURLConnection.setFollowRedirects(false);
            // note : you may also need
            // HttpURLConnection.setInstanceFollowRedirects(false)
            HttpURLConnection con = (HttpURLConnection) new URL(URLName)
                    .openConnection();
            con.setRequestMethod("HEAD");

            return (con.getResponseCode() == HttpURLConnection.HTTP_OK);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    static double round(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.###");
        return Double.valueOf(twoDForm.format(d));
    }

Sorry about that... That code was too long. Anyway. To test this, right now it will check if version 0.009 is available. Which it is. Its full version is in the variable double origVersion. Now, if you set origVersion to 0.009, it will check for 0.01. Which is fine, except for the fact that App_0.01.zip DOES NOT exist, and yet it still says it does!

I have also looked into wget to solve this problem, by starting up wget with the arguments

THEFILENAME --no-proxy --spider

but that didn't work either. Can anyone help me? I would greatly appreciate it.

I also saw somewhere else that you could establish a connection with the file and if it secures, the file exists. If not, it doesn't. However, I have no idea how to do that. Can anyone bring me out of the dark?

[EDIT]

Also, running the THEFILENAME --no-proxy --spider on wget worked, and outputted the following, when checking for version 0.009:

Spider mode enabled. Check if remote file exists.
--2012-03-16 08:59:55--  http://dl.dropbox.com/u/.../....zip
Resolving dl.dropbox.com... 107.21.103.249, 107.20.135.4, 107.20.198.68, ...
Connecting to dl.dropbox.com|107.21.103.249|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 758067 (740K) [application/zip]
Remote file exists.

And when checking for version 0.01:

Spider mode enabled. Check if remote file exists.
--2012-03-16 09:01:15--  http://dl.dropbox.com/u/.../....zip
Resolving dl.dropbox.com... 107.22.196.64, 50.19.217.32, 174.129.218.194, ...
Connecting to dl.dropbox.com|107.22.196.64|:80... connected.
HTTP request sent, awaiting response... 404 NOT FOUND
Remote file does not exist -- broken link!!!

I also tried reading the output of wget using this, and using if(input.indexOf("404 NOT FOUND") == -1), but still to no avail.

Community
  • 1
  • 1
Xyene
  • 2,304
  • 20
  • 36
  • *"I have recently been trying to create an updater for my program."* If the app. has a GUI, launch it using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). Automatic updates is just one of the features offered by JWS. – Andrew Thompson Mar 16 '12 at 13:03
  • Thanks! I'll look into it! However, would webstart work if its a console program? Because it is... – Xyene Mar 16 '12 at 13:05

1 Answers1

0

Doing a HEAD request is definitely the right way to check if a file exists on a remote server.

I can't see anything wrong with your exists(String URLName) method so I would check that it is being passed the URL you think it is.

What looks odd to be is this line:

versionTimes = round(versionTimes + 0.001);

You're changing the value of versionTimes in between checking for the existence of the file and printing your message.

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • Thanks for the reply! I used System.out.println("http://dl.dropbox.com/u/67341745/" + "HAL_"+ String.valueOf(origVersion + versionTimes) + ".zip"") as you said, and it is going to the correct location. I also placed the versionTimes addition before the while(exists), and it still doesn't work. Could it be that dropbox disables spiders? – Xyene Mar 16 '12 at 13:27
  • I'd put `System.out.println(URLName + " = + con.getResponseCode())` in `exists()` so you can see a 200 or 404 against the specific URLs. – David Webb Mar 16 '12 at 13:33
  • Hmm... that didn't work either...Ah well. I'll just rewrite my entire program in C++... its not as hard to update things like that. Anyway, Thanks for your help! – Xyene Mar 16 '12 at 13:50