Why does Java automatically decode / in URI encoded filenames?

后端 未结 2 1954
囚心锁ツ
囚心锁ツ 2021-01-18 06:43

I have a servlet that needs to write out files that have a user-configurable name. I am trying to use URI encoding to properly escape special characters, but the JRE appear

2条回答
  •  别那么骄傲
    2021-01-18 07:33

    The new File(URI) constructs the file based on the path as obtained by URI#getPath() instead of -what you expected- URI#getRawPath(). This look like a feature "by design".

    You have 2 options:

    1. Run URLEncoder#encode() on fn twice (note: encode(), not encoder()).
    2. Use new File(String) instead.

提交回复
热议问题