簡體   English   中英

如何從jar中獲取jar URL:包含“!”的URL和jar中的特定文件?

[英]How do I get just the jar URL from a jar: URL containing a “!” and a specific file in the jar?

我在運行時獲得了一個jar文件URL:

jar:file:///C:/proj/parser/jar/parser.jar!/test.xml

如何將其轉換為有效路徑:

C:/proj/parser/jar/parser.jar.

我已經嘗試使用File(URI)getPath()getFile()是徒勞的。

如果MS-Windows沒有被前導斜杠冒犯,這可能會這樣做:

    final URL jarUrl =
        new URL("jar:file:/C:/proj/parser/jar/parser.jar!/test.xml");
    final JarURLConnection connection =
        (JarURLConnection) jarUrl.openConnection();
    final URL url = connection.getJarFileURL();

    System.out.println(url.getFile());

不確定能給你什么的確切方法,但這應該讓你接近:

import static org.junit.Assert.assertEquals;

import java.net.URL;

import org.junit.Test;

public class UrlTest {

    @Test
    public void testUrl() throws Exception {
        URL jarUrl = new URL("jar:file:/C:/proj/parser/jar/parser.jar!/test.xml");
        assertEquals("jar", jarUrl.getProtocol());
        assertEquals("file:/C:/proj/parser/jar/parser.jar!/test.xml", jarUrl.getFile());
        URL fileUrl = new URL(jarUrl.getFile());
        assertEquals("file", fileUrl.getProtocol());
        assertEquals("/C:/proj/parser/jar/parser.jar!/test.xml", fileUrl.getFile());
        String[] parts = fileUrl.getFile().split("!");
        assertEquals("/C:/proj/parser/jar/parser.jar", parts[0]);
    }
}

希望這可以幫助。

有些人可能認為這有點'hacky',但它會在那個實例中完成工作,我確信它比在其他建議中創建所有這些對象更好看。

String jarUrl = "jar:file:/C:/proj/parser/jar/parser.jar!/test.xml";

jarUrl = jarUrl.substring(jarUrl.indexOf('/')+1, jarUrl.indexOf('!'));

此解決方案將處理路徑中的空間。

String url = "jar:file:/C:/dir%20with%20spaces/myjar.jar!/resource";
String fileUrl = url.substring(4, url.indexOf('!'));
File file = new File(new URL(fileUrl).toURI());
String fileSystemPath = file.getPath();

或者以URL對象開頭:

...
String fileUrl = url.getPath().substring(0, url.indexOf('!'));
...

我只是必須這樣做。

        URL url = clazz.getResource(clazz.getSimpleName() + ".class");
        String proto = url.getProtocol();
        boolean isJar = proto.equals("jar"); // see if it's in a jar file URL

        if(isJar)
        {
            url = new URL(url.getPath()); // this nicely strips off the leading jar: 
            proto = url.getProtocol();
        }

        if(proto.equals("file"))
        {
             if(isJar) 
                // you can truncate it at the last '!' here
        } 
        else if(proto == "http") {
            ...
  //This code will work on both Windows and Linux
  public String path()
  {
  URL url1 = getClass().getResource("");
  String urs=url1.toString();
  urs=urs.substring(9);
  String truepath[]=urs.split("parser.jar!");
  truepath[0]=truepath[0]+"parser.jar";
  truepath[0]=truepath[0].replaceAll("%20"," ");
  return truepath[0];
  }

你可以這樣做。這很有效

    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);
    }
    return filePath[1];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM