我最近一直在尝试为我的程序创建更新程序。更新程序应该转到dropbox,查看“public”文件夹中的文件,并确定它是否存在。它可以工作,并且可以下载文件,但是如果文件存在则无法检查。我看到this,我认为这是一个解决方案,但它似乎没有起作用。
以下是我用来检查文件存在的代码:
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;
}
}
然而,似乎总是回归真实。我正在访问的文件都以“App_”开头,以“.zip”结尾。唯一不同的是版本,它是#。###格式。
以下是我如何检查它的完整代码:
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));
}
很抱歉......那段代码太长了。无论如何。为了测试这一点,现在它将检查版本0.009是否可用。它是什么。它的完整版本是变量double origVersion。现在,如果将origVersion设置为0.009,它将检查0.01。哪个好,除了App_0.01.zip不存在,但它仍然说它确实存在!
我也通过使用参数
启动wget来研究wget来解决这个问题THEFILENAME --no-proxy --spider
但这也不起作用。谁能帮我?我将不胜感激。
我还在其他地方看到你可以与文件建立连接,如果它保密,文件就存在了。如果没有,它就没有。但是,我不知道该怎么做。谁能把我带出黑暗?
[编辑]
另外,在wget上运行THEFILENAME --no-proxy --spider工作,并在检查版本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.
检查版本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!!!
我也尝试使用this读取wget的输出,并使用if(input.indexOf(“404 NOT FOUND”)== -1),但仍无济于事。
答案 0 :(得分:0)
执行HEAD
请求绝对是检查远程服务器上是否存在文件的正确方法。
我看不出你的exists(String URLName)
方法有什么问题,所以我会检查它是否传递了你认为的URL。
看起来奇怪的是这一行:
versionTimes = round(versionTimes + 0.001);
您正在检查文件是否存在和打印邮件之间更改versionTimes
的值。