From d8651a88ecac9d6e9f2cda6100032666705fb96b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 13 Feb 2009 12:16:11 +0000 Subject: [PATCH] UrlResource eagerly closes HTTP connections in case of "getInputStream()" failure (SPR-5338) --- .../org/springframework/core/io/UrlResource.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/UrlResource.java b/org.springframework.core/src/main/java/org/springframework/core/io/UrlResource.java index 0406b33d97d..912b9091c3a 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/UrlResource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/UrlResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.core.io; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; @@ -120,7 +121,16 @@ public class UrlResource extends AbstractResource { public InputStream getInputStream() throws IOException { URLConnection con = this.url.openConnection(); con.setUseCaches(false); - return con.getInputStream(); + try { + return con.getInputStream(); + } + catch (IOException ex) { + // Close the HTTP connection (if applicable). + if (con instanceof HttpURLConnection) { + ((HttpURLConnection) con).disconnect(); + } + throw ex; + } } /**