fixed exists() check for resources in zipped files (SPR-7559)

This commit is contained in:
Juergen Hoeller 2010-09-29 14:45:44 +00:00
parent f88f69e700
commit e211c09065
1 changed files with 23 additions and 7 deletions

View File

@ -18,6 +18,7 @@ package org.springframework.core.io;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
@ -95,14 +96,28 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
// Try a URL connection content-length header... // Try a URL connection content-length header...
URLConnection con = url.openConnection(); URLConnection con = url.openConnection();
con.setUseCaches(false); con.setUseCaches(false);
if (con instanceof HttpURLConnection) { HttpURLConnection httpCon =
((HttpURLConnection) con).setRequestMethod("HEAD"); (con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
if (httpCon != null) {
httpCon.setRequestMethod("HEAD");
if (httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) {
return true;
}
} }
boolean doesExist = (con.getContentLength() >= 0); if (con.getContentLength() >= 0) {
if (!doesExist && con instanceof HttpURLConnection) { return true;
((HttpURLConnection) con).disconnect(); }
if (httpCon != null) {
// no HTTP OK status, and no content-length header: give up
httpCon.disconnect();
return false;
}
else {
// Fall back to stream existence: can we open the stream?
InputStream is = getInputStream();
is.close();
return true;
} }
return doesExist;
} }
} }
catch (IOException ex) { catch (IOException ex) {
@ -178,4 +193,5 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
return new VfsResource(VfsUtils.getRoot(uri)); return new VfsResource(VfsUtils.getRoot(uri));
} }
} }
}
}