@@ -47,6 +47,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
4747
4848 @ Override
4949 public boolean exists () {
50+ HttpURLConnection httpCon = null ;
5051 try {
5152 URL url = getURL ();
5253 if (ResourceUtils .isFileURL (url )) {
@@ -58,16 +59,14 @@ public boolean exists() {
5859 URLConnection con = url .openConnection ();
5960 customizeConnection (con );
6061
61- HttpURLConnection httpCon = (con instanceof HttpURLConnection huc ? huc : null );
62+ httpCon = (con instanceof HttpURLConnection huc ? huc : null );
6263 if (httpCon != null ) {
6364 httpCon .setRequestMethod ("HEAD" );
6465 int code = httpCon .getResponseCode ();
6566 if (code == HttpURLConnection .HTTP_OK ) {
66- httpCon .disconnect ();
6767 return true ;
6868 }
6969 else if (code == HttpURLConnection .HTTP_NOT_FOUND ) {
70- httpCon .disconnect ();
7170 return false ;
7271 }
7372 else if (code == HttpURLConnection .HTTP_BAD_METHOD ) {
@@ -76,11 +75,9 @@ else if (code == HttpURLConnection.HTTP_BAD_METHOD) {
7675 if (con instanceof HttpURLConnection newHttpCon ) {
7776 code = newHttpCon .getResponseCode ();
7877 if (code == HttpURLConnection .HTTP_OK ) {
79- newHttpCon .disconnect ();
8078 return true ;
8179 }
8280 else if (code == HttpURLConnection .HTTP_NOT_FOUND ) {
83- newHttpCon .disconnect ();
8481 return false ;
8582 }
8683 httpCon = newHttpCon ;
@@ -109,7 +106,6 @@ else if (con.getContentLengthLong() > 0) {
109106
110107 if (httpCon != null ) {
111108 // No HTTP OK status, and no content-length header: give up
112- httpCon .disconnect ();
113109 return false ;
114110 }
115111 else {
@@ -122,6 +118,11 @@ else if (con.getContentLengthLong() > 0) {
122118 catch (IOException ex ) {
123119 return false ;
124120 }
121+ finally {
122+ if (httpCon != null ) {
123+ httpCon .disconnect ();
124+ }
125+ }
125126 }
126127
127128 @ Override
0 commit comments