简体   繁体   中英

Java ClassLoader getResource with special characters in path

I have the following problem. In our integration tests we use a different config which will be loaded from test resources with the following code prior to the tests:

URL resource = ClassLoader.getSystemResource("application.conf");

This works fine as long as there are no special characters in path. For example having the following correct path

D:/Dev/projects/#FLI/flinsta/fgraph/build/resources/test/application.conf

will result in the following wrong file path given by getSystemResource :

D:/Dev/projects/%23FLI/flinsta/fgraph/build/resources/test/application.conf

This then results in a file which simply doesn't exist. How can I make sure that something like this does not happen. Renaming the path is an option. However I would like to find a solution instead of a workaround.

Thank you for any help!

To answer my own question with the help of the comments:

URL resource = ClassLoader.getSystemResource("application.conf");
String configPath = URLDecoder.decode(resource.getFile(), "UTF-8");

Variable configPath then contains the correct path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM