diff --git a/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java b/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java index 9f19b76d9971..ad236509d88d 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java @@ -139,10 +139,15 @@ public boolean isFile() { return true; } else { - return (this.servletContext.getRealPath(this.path) != null); + String realPath = this.servletContext.getRealPath(this.path); + if (realPath == null) { + return false; + } + File file = new File(realPath); + return (file.exists() && file.isFile()); } } - catch (MalformedURLException ex) { + catch (IOException ex) { return false; } } diff --git a/spring-web/src/test/java/org/springframework/web/context/support/ResourceTests.java b/spring-web/src/test/java/org/springframework/web/context/support/ResourceTests.java index 340838871814..937885e107f7 100644 --- a/spring-web/src/test/java/org/springframework/web/context/support/ResourceTests.java +++ b/spring-web/src/test/java/org/springframework/web/context/support/ResourceTests.java @@ -36,6 +36,8 @@ public void testServletContextResource() throws IOException { MockServletContext sc = new MockServletContext(); Resource resource = new ServletContextResource(sc, "org/springframework/core/io/Resource.class"); doTestResource(resource); + Resource resourceNotExists = new ServletContextResource(sc, "org/springframework/core/io/ResourceNotExists.class"); + doTestNotExistsResource(resourceNotExists); assertThat(new ServletContextResource(sc, "org/springframework/core/../core/io/./Resource.class")).isEqualTo(resource); } @@ -48,6 +50,9 @@ public void testServletContextResourceWithRelativePath() throws IOException { } private void doTestResource(Resource resource) throws IOException { + assertThat(resource.getFile()).isNotNull(); + assertThat(resource.exists()).isTrue(); + assertThat(resource.isFile()).isTrue(); assertThat(resource.getFilename()).isEqualTo("Resource.class"); assertThat(resource.getURL().getFile().endsWith("Resource.class")).isTrue(); @@ -61,4 +66,9 @@ private void doTestResource(Resource resource) throws IOException { assertThat(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class")).isTrue(); assertThat(relative2.exists()).isTrue(); } + + private void doTestNotExistsResource(Resource resource) throws IOException { + assertThat(resource.exists()).isFalse(); + assertThat(resource.isFile()).isFalse(); + } }