fixed exists() check for resources in zipped files (SPR-7559)
This commit is contained in:
parent
f88f69e700
commit
e211c09065
|
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue