我想检查文件是否存在于服务器上,如果存在则将boolean值设置为true并使用它的值超出线程。
这是我的代码:
new Thread() {
public void run() {
try {
HttpURLConnection.setFollowRedirects(false);
// note : you may also need
//HttpURLConnection.setInstanceFollowRedirects(false)
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
if( (con.getResponseCode() == HttpURLConnection.HTTP_OK) ) {
fileExists = true;
}
else {
fileExists = false;
}
catch (Exception e) {
e.printStackTrace();
log.d("FILE_EXISTS", "false");;
}
}
}.start();
但传递此代码后,布尔值为false,我确定服务器上存在该文件!!