[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]JarURLConnection的空格问题

上一篇:[备忘]java -jar 远程调试的写法
下一篇:[备忘]tomcat8中URI不支持特殊字符解决方案

添加日期:2021/6/1 16:22:25 快速返回   返回列表 阅读864次
简写了这样的程序:


import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class TestJar {

    public static void main(String[] args) throws IOException {
        String path = "file:/D:/unpkg/xxx-1.0.jar!/BOOT-INF/classes!/";
        String path2 = "file:/D:/Program%20Files%20(x86)/xx/bin/xxx-1.0.jar!/BOOT-INF/classes!/";
        invokeRegisterAllConfigToMap(path);
        System.out.println("--------------");
        invokeRegisterAllConfigToMap(path2);
        
    }
    
    public static void invokeRegisterAllConfigToMap(String path) throws IOException {
        URL url = new URL("jar:" + path);
       JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();
        JarFile jarFile = jarURLConnection.getJarFile();
        Enumeration entries = jarFile.entries();

        while(entries.hasMoreElements()) {
            JarEntry jarEntry = (JarEntry)entries.nextElement();
            String fileName = jarEntry.getName();
            //if (fileName.endsWith("properties") && fileName.indexOf("_message_") > 0) {
                System.out.println(fileName);
            //}
        }

    }

}


发现不支持最后的!/,去掉就可以。
而在springboot工程里,最后有!/却可以,莫名其妙。

研究半天,发现不一样。

----------------------

(1)参考:
https://docs.oracle.com/javase/7/docs/api/java/net/JarURLConnection.html
JarURLConnection是A URL Connection to a Java ARchive (JAR) file or an entry in a JAR file.


A Jar entry
jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class

A Jar file
jar:http://www.foo.com/bar/baz.jar!/

A Jar directory
jar:http://www.foo.com/bar/baz.jar!/COM/foo/

!/ 是分隔符。

只有jar后面有个!/。

(2)spring扩展了它,org.springframework.boot.loader.jar.JarURLConnection
支持俩!/吗?
jar:http://www.foo.com/bar/baz.jar!/COM/foo!/这种?

(3)
在spring boot里执行时
URL url = new URL("jar:" + path);
JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();

实际走的:
org.springframework.boot.loader.jar.Handler
---------------
@Override
protected URLConnection openConnection(URL url) throws IOException {
  if (this.jarFile != null && isUrlInJarFile(url, this.jarFile)) {
    return JarURLConnection.get(url, this.jarFile);
  }
  try {
    return JarURLConnection.get(url, getRootJarFileFromUrl(url));
  }
  catch (Exception ex) {
    return openFallbackConnection(url, ex);
  }
}
---------------
大概意思,应该是出错了,就转为兜底的Connection了,也就是java的
sun.net.www.protocol.jar.JarURLConnection

(4)当路径没有空格时,是没问题的,正常执行。
 jarURLConnection实际是org.springframework.boot.loader.jar.JarURLConnection。

(5)当路径有空格时,转为兜底的sun.net.www.protocol.jar.JarURLConnection。
然后它不支持两个分隔符,就会报错。坑爹。

(6)怎么解决?
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved