无法在以感叹号结尾的目录中打开资源(!) [英] Unable to open resources in directories which end with an exclamation mark (!)

查看:343
本文介绍了无法在以感叹号结尾的目录中打开资源(!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



原因是 jar / URL中的!/被解释为JAR文件名和JAR本身内的路径之间的分隔符。如果目录名以!结尾,则目录末尾的!/字符序列将被错误地解释。在您的情况下,您实际上是尝试使用以下URL访问资源:



jar:file:/// d:/ test1231 @#!!! / test .jar!/images/Logo.png



该漏洞已经开放了近12年,不太可能被修复。实际上我不知道如何在不破坏其他事情的情况下修复它。问题是设计决定使用!作为JAR文件的URL方案中具有特殊含义(分隔符)的字符:

  jar:< JAR文件的URL> ;!/< JAR文件中的路径> 

由于感叹号是URL中允许的字符,因此它可能出现在JAR的URL中文件本身,以及JAR文件中的路径,使得在某些情况下无法找到实际的!/分隔符。


The getResourceAsStream-method returns null whenever running the executable jar in a directory which ends with a exclamation mark.

For the following example, I have a Eclipse project the following directory structure:

src\ (Source Folder)
    main\ (Package)
        Main.java
    res\ (Source Folder)
        images\
            Logo.png

I'm reading the Logo.png as follows:

public static void main(String[] args) throws IOException {
    try (InputStream is = Main.class.getClassLoader().getResourceAsStream("images/Logo.png")) {
        Image image = ImageIO.read(is);
        System.out.println(image);
    }   
}

See the attachment for 2 test cases. First, the executable jar is started from the directory "D:\test123!@#" without any problems. Secondly, the executable jar is started from the directory "D:\test123!@#!!!", with problems.

Are directories ending with an exclamation mark not supported? Is the code wrong?

Thanks in advance.

解决方案

Probably because of this bug or any of the many similar bugs in the Java bug database:

http://bugs.sun.com/view_bug.do?bug_id=4523159

The reason is that "!/" in a jar URL is interpreted as the separator between the JAR file name and the path within the JAR itself. If a directory name ends with !, the "!/" character sequence at the end of the directory is incorrectly interpreted. In your case, you are actually trying to access a resource with the following URL:

jar:file:///d:/test1231@#!!!/test.jar!/images/Logo.png

The bug has been open for almost 12 years and is not likely to be fixed. Actually I don't know how it can be fixed without breaking other things. The problem is the design decision to use ! as a character with a special meaning (separator) in the URL scheme for JAR files:

jar:<URL for JAR file>!/<path within the JAR file>

Since the exclamation mark is an allowed character in URLs, it may occur both in the URL to the JAR file itself, as well as in the path within the JAR file, making it impossible in some cases to find the actual "!/" separator.

这篇关于无法在以感叹号结尾的目录中打开资源(!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆