在Java中,你可以使用相同的API但使用不同的URL协议加载各种资源:

file:///tmp.txt
http://127.0.0.1:8080/a.properties
jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class

这很好地将资源的实际加载与需要资源的应用程序分离开来,而且由于URL只是一个字符串,资源加载也非常容易配置。

是否存在使用当前类加载器加载资源的协议? 这与Jar协议类似,只是我不需要知道资源来自哪个Jar文件或类文件夹。

当然,我可以使用Class.getResourceAsStream(“a.xml”)做到这一点,但这需要我使用不同的API,因此需要对现有代码进行更改。我希望能够在所有可以为资源指定URL的地方使用它,只需更新属性文件即可。


当前回答

我试图避免URL类,而是依赖URI。因此,对于需要URL的事情,我想做Spring资源查找,我做以下工作:

public static URL toURL(URI u, ClassLoader loader) throws MalformedURLException {
    if ("classpath".equals(u.getScheme())) {
        String path = u.getPath();
        if (path.startsWith("/")){
            path = path.substring("/".length());
        }
        return loader.getResource(path);
    }
    else if (u.getScheme() == null && u.getPath() != null) {
        //Assume that its a file.
        return new File(u.getPath()).toURI().toURL();
    }
    else {
        return u.toURL();
    }
}

要创建一个URI,可以使用URI.create(..)。这种方式也更好,因为您控制了将执行资源查找的ClassLoader。

我注意到其他一些答案试图将URL解析为字符串以检测该方案。我认为最好是传递URI并使用它来解析。

事实上,我在不久前提出了一个问题,要求Spring Source将他们的资源代码从核心中分离出来,这样你就不需要所有其他Spring的东西了。

其他回答

(类似于Azder的回答,但策略略有不同。)

我认为对于类路径中的内容没有预定义的协议处理程序。(所谓的类路径:协议)。

但是,Java允许您添加自己的协议。这是通过提供具体实现java.net.URLStreamHandler和java.net.URLConnection来实现的。

本文描述了如何实现自定义流处理程序: http://java.sun.com/developer/onlineTraining/protocolhandlers/。

介绍和基本实现

首先,您至少需要一个URLStreamHandler。这实际上会打开到给定URL的连接。注意,这被简单地称为Handler;这允许你指定java -Djava.protocol.handler.pkgs=org.my。协议,它将自动被拾取,使用“简单”包名作为支持的协议(在本例中为“classpath”)。

使用

new URL("classpath:org/my/package/resource.extension").openConnection();

Code

package org.my.protocols.classpath;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

/** A {@link URLStreamHandler} that handles resources on the classpath. */
public class Handler extends URLStreamHandler {
    /** The classloader to find resources from. */
    private final ClassLoader classLoader;

    public Handler() {
        this.classLoader = getClass().getClassLoader();
    }

    public Handler(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    protected URLConnection openConnection(URL u) throws IOException {
        final URL resourceUrl = classLoader.getResource(u.getPath());
        return resourceUrl.openConnection();
    }
}

启动问题如果你和我一样,你不想依赖于在启动中设置的属性来到达某个地方(在我的情况下,我喜欢像Java WebStart那样保持我的选项开放-这就是为什么我需要所有这些)。

解决方案/增强

手动代码处理程序规范

如果你能控制代码,你就能做到

new URL(null, "classpath:some/package/resource.extension", new org.my.protocols.classpath.Handler(ClassLoader.getSystemClassLoader()))

这将使用您的处理程序打开连接。

但同样,这不是令人满意的,因为你不需要一个URL来做这件事——你想这样做是因为一些你不能(或不想)控制的库想要URL…

JVM Handler注册

最终的选择是注册一个URLStreamHandlerFactory,它将处理jvm中的所有url:

package my.org.url;

import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import java.util.HashMap;
import java.util.Map;

class ConfigurableStreamHandlerFactory implements URLStreamHandlerFactory {
    private final Map<String, URLStreamHandler> protocolHandlers;

    public ConfigurableStreamHandlerFactory(String protocol, URLStreamHandler urlHandler) {
        protocolHandlers = new HashMap<String, URLStreamHandler>();
        addHandler(protocol, urlHandler);
    }

    public void addHandler(String protocol, URLStreamHandler urlHandler) {
        protocolHandlers.put(protocol, urlHandler);
    }

    public URLStreamHandler createURLStreamHandler(String protocol) {
        return protocolHandlers.get(protocol);
    }
}

要注册处理程序,请使用已配置的工厂调用URL.setURLStreamHandlerFactory()。然后像第一个例子那样输入新的URL("classpath:org/my/package/resource.extension"),然后就可以继续了。

JVM处理器注册问题

请注意,每个JVM只能调用该方法一次,并且请注意Tomcat将使用此方法注册JNDI处理程序(AFAIK)。试试Jetty(我会的);在最坏的情况下,您可以先使用方法,然后它必须在您周围工作!

许可证

我将此发布到公共领域,并询问如果您希望修改,您将在某处启动OSS项目并在这里评论详细信息。一个更好的实现是有一个URLStreamHandlerFactory,它使用ThreadLocals来存储每个Thread.currentThread(). getcontextclassloader()的URLStreamHandlers。我甚至会给你我的修改和测试课程。

当然,使用注册URLStreamHandlers的解决方案是最正确的,但有时需要最简单的解决方案。所以,我使用下面的方法:

/**
 * Opens a local file or remote resource represented by given path.
 * Supports protocols:
 * <ul>
 * <li>"file": file:///path/to/file/in/filesystem</li>
 * <li>"http" or "https": http://host/path/to/resource - gzipped resources are supported also</li>
 * <li>"classpath": classpath:path/to/resource</li>
 * </ul>
 *
 * @param path An URI-formatted path that points to resource to be loaded
 * @return Appropriate implementation of {@link InputStream}
 * @throws IOException in any case is stream cannot be opened
 */
public static InputStream getInputStreamFromPath(String path) throws IOException {
    InputStream is;
    String protocol = path.replaceFirst("^(\\w+):.+$", "$1").toLowerCase();
    switch (protocol) {
        case "http":
        case "https":
            HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();
            int code = connection.getResponseCode();
            if (code >= 400) throw new IOException("Server returned error code #" + code);
            is = connection.getInputStream();
            String contentEncoding = connection.getContentEncoding();
            if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip"))
                is = new GZIPInputStream(is);
            break;
        case "file":
            is = new URL(path).openStream();
            break;
        case "classpath":
            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path.replaceFirst("^\\w+:", ""));
            break;
        default:
            throw new IOException("Missed or unsupported protocol in path '" + path + "'");
    }
    return is;
}

我认为这是值得回答的问题——如果您正在使用Spring,那么您已经有了这个功能

Resource firstResource =
    context.getResource("http://www.google.fi/");
Resource anotherResource =
    context.getResource("classpath:some/resource/path/myTemplate.txt");

就像春季文档中解释的,斯卡夫曼在评论中指出的那样。

灵感来自@Stephen https://stackoverflow.com/a/1769454/980442 和http://docstore.mik.ua/orelly/java/exp/ch09_06.htm

使用

new URL("classpath:org/my/package/resource.extension").openConnection()

只需将这个类创建到sun.net.www.protocol.classpath包中,并将其运行到Oracle JVM实现中即可。

package sun.net.www.protocol.classpath;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

public class Handler extends URLStreamHandler {

    @Override
    protected URLConnection openConnection(URL u) throws IOException {
        return Thread.currentThread().getContextClassLoader().getResource(u.getPath()).openConnection();
    }
}

如果您正在使用另一个JVM实现,请设置java.protocol.handler.pkgs=sun.net.www.protocol系统属性。

仅供参考: URL http://docs.oracle.com/javase/7/docs/api/java/net/URL.html(以% 20以% 20 int, % 20以)