From 75dd8d9c0688331c0ca06b2e5cd497be49af0d3a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 6 Apr 2017 14:07:22 +0200 Subject: [PATCH] UrlResource applies StringUtils.getFilename against URL path Issue: SPR-15411 --- .../main/java/org/springframework/core/io/UrlResource.java | 7 +++---- .../java/org/springframework/core/io/ResourceTests.java | 7 ++++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/UrlResource.java b/spring-core/src/main/java/org/springframework/core/io/UrlResource.java index 6c5f2f5878..c3d440c478 100644 --- a/spring-core/src/main/java/org/springframework/core/io/UrlResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/UrlResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -239,12 +239,11 @@ public class UrlResource extends AbstractFileResolvingResource { /** * This implementation returns the name of the file that this URL refers to. - * @see java.net.URL#getFile() - * @see java.io.File#getName() + * @see java.net.URL#getPath() */ @Override public String getFilename() { - return new File(this.url.getFile()).getName(); + return StringUtils.getFilename(this.url.getPath()); } /** diff --git a/spring-core/src/test/java/org/springframework/core/io/ResourceTests.java b/spring-core/src/test/java/org/springframework/core/io/ResourceTests.java index bb487fad98..d46500f87e 100644 --- a/spring-core/src/test/java/org/springframework/core/io/ResourceTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/ResourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -133,8 +133,13 @@ public class ResourceTests { Resource resource = new UrlResource(getClass().getResource("Resource.class")); doTestResource(resource); assertEquals(new UrlResource(getClass().getResource("Resource.class")), resource); + Resource resource2 = new UrlResource("file:core/io/Resource.class"); assertEquals(resource2, new UrlResource("file:core/../core/io/./Resource.class")); + + assertEquals("test.txt", new UrlResource("file:/dir/test.txt?argh").getFilename()); + assertEquals("test.txt", new UrlResource("file:\\dir\\test.txt?argh").getFilename()); + assertEquals("test.txt", new UrlResource("file:\\dir/test.txt?argh").getFilename()); } private void doTestResource(Resource resource) throws IOException {