Browse Source

UrlResource applies StringUtils.getFilename against URL path

Issue: SPR-15411
pull/1377/head
Juergen Hoeller 9 years ago
parent
commit
75dd8d9c06
  1. 7
      spring-core/src/main/java/org/springframework/core/io/UrlResource.java
  2. 7
      spring-core/src/test/java/org/springframework/core/io/ResourceTests.java

7
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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. * This implementation returns the name of the file that this URL refers to.
* @see java.net.URL#getFile() * @see java.net.URL#getPath()
* @see java.io.File#getName()
*/ */
@Override @Override
public String getFilename() { public String getFilename() {
return new File(this.url.getFile()).getName(); return StringUtils.getFilename(this.url.getPath());
} }
/** /**

7
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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")); Resource resource = new UrlResource(getClass().getResource("Resource.class"));
doTestResource(resource); doTestResource(resource);
assertEquals(new UrlResource(getClass().getResource("Resource.class")), resource); assertEquals(new UrlResource(getClass().getResource("Resource.class")), resource);
Resource resource2 = new UrlResource("file:core/io/Resource.class"); Resource resource2 = new UrlResource("file:core/io/Resource.class");
assertEquals(resource2, new UrlResource("file:core/../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 { private void doTestResource(Resource resource) throws IOException {

Loading…
Cancel
Save