Java - special characters URL

I have a function:

public static ImageIcon GetIconImageFromResource(String path){ URL url = ARMMain.class.getResource(path); return new ImageIcon(url); } 

This works fine, but if I put my .JAR file, for example, C: \ Ares !!! \ myfile.jar , it does not work, and I do not see my icons. I noticed that my URL file is: / C: / Ares !!! / myfile.jar! /Com/test/images/img.png . Therefore, after the JAR extension, there is a symbol ! ! I think this is the main reason! What can I do to avoid this problem? Thanks.

+6
source share
2 answers

This is apparently a Java problem (more than an error). The JAR URL uses !/ As a delimiter, but does not escape it when it appears inside the file path.

You can try to escape from it yourself, turn Ares!!!/ into Ares!!%21/ , but save myfile.jar!/ As it is.


According to RFC3986,! is a reserved char delimiter. Therefore, it is safe to use it to separate different parts in a URL; when it appears inside the part, it must be escaped as %21

However, the Java code associated with the JAR URL seems to follow the older RFC2396 in which ! was an unconditional char. For Java, it was unreasonable to choose an unconditional char as a delimiter. And when he did, he should at least internally escape it, as if a char were reserved.

+5
source

Take a look at the URLEncoder class.

-1
source

Source: https://habr.com/ru/post/905592/


All Articles