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.