Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-3900

FileSystemResource.getURL should return "file:/C:/foo%20bar" instead of "file:C:/foo bar"

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.5 RC1
    • Component/s: Core
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      This is related to SPR-3899.

      Here's a small test that compares how the JDK classes convert a File to a URL and a URI and how FileSystemResource does it:

      URL fileUrl = new File("C:/foo bar").toURI().toURL();
      URL springUrl = new FileSystemResource("C:/foo bar").getURL();
      System.out.println(fileUrl);
      System.out.println(springUrl);
      System.out.println(fileUrl.toURI());
      System.out.println(springUrl.toURI());

      prints

      file:/C:/foo%20bar
      file:C:/foo bar
      file:/C:/foo%20bar
      Exception in thread "main" java.net.URISyntaxException: Illegal character in opaque part at index 11: file:C:/foo bar

      Because whitespace is not escaped in the URL, it cannot be converted to a URI (which uses stricter parsing).

      The method java.io.File.toURL() has the same problem, which is why it is deprecated since JDK 6.

      If JDK 1.4 can be used, FileSystemResource.getURL() can easily be fixed - just return this.file.toURI().toURL().

      With a pre-1.4 JDK, it's not easy to fix this. Using a URLEncoder to encode the path before passing it to the URL constructor does not work because that would also URL-encode the slashes...

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                juergen.hoeller Juergen Hoeller
                Reporter:
                jcsahnwaldt Christopher Sahnwaldt
                Last updater:
                Trevor Marshall
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  11 years, 6 weeks, 2 days ago