【问题标题】:How do I convert a "jar:file" URI to the path of Jar file?如何将“jar:file”URI 转换为 Jar 文件的路径?
【发布时间】:2011-11-04 18:30:08
【问题描述】:

如何从jar:file:/C:/Program%20Files/test.jar!/foo/bar 转到指向C:/Program Files/test.jarFile

【问题讨论】:

    标签: java uri


    【解决方案1】:

    以下代码适用于我(基于How do I get just the jar URL from a jar: URL containing a "!" and a specific file in the jar?):

    URL url = new URL("jar:file:/C:/Program%20Files/test.jar!/foo/bar");
    JarURLConnection connection = (JarURLConnection) url.openConnection();
    File file = new File(connection.getJarFileURL().toURI())
    

    【讨论】:

      【解决方案2】:

      你可以这样做。这行得通

      ClassLoader loader = this.getClass().getClassLoader();
      URL url = loader.getResource("resource name");
      String[] filePath = null;
      String protocol = url.getProtocol();
      if(protocol.equals("jar")){
          url = new URL(url.getPath());
          protocol = url.getProtocol();
      }
      if(protocol.equals("file")){
          String[] pathArray = url.getPath().split("!");
          filePath = pathArray[0].split("/",2);
      }
      

      所需文件 = new File(FilePath[1]);

      【讨论】:

        猜你喜欢
        • 2015-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-09
        • 1970-01-01
        • 2015-02-20
        • 1970-01-01
        相关资源
        最近更新 更多