找不到文件配置.xml在空:无协议:配置.如何设置配置的路径.xml



所以我正在开发一个使用HTML,CSS和AngularJS的基本Web应用程序,并且我已经开始使用Spring添加功能。当我添加一些 JAR 时,我开始收到错误 DEBUG DefaultFileSystem - Could not locate file config.xml at null: no protocol: config.xml似乎找不到我的配置.xml文档,该文档目前位于src/main/resources,但是我已经尝试了其他地址。我在哪里可以设置我有文件的路径,而不是让它被null

在 Java 中资源加载可能很棘手,下面的类将在桌面、web、类路径或当前工作目录中为您加载资源:

package test;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class JKResourceLoader {
    public InputStream getResourceAsStream(String resourceName) {
        URL url = getResourceUrl(resourceName);
        if (url != null) {
            try {
                return url.openStream();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
    public URL getResourceUrl(String fileName) {
        if (fileName == null) {
            return null;
        }
        URL resource = getClass().getResource(fileName);
        if (resource == null) {
            resource = Thread.currentThread().getContextClassLoader().getResource(fileName);
            if (resource == null) {
                resource = ClassLoader.getSystemResource(fileName);
                if (resource == null) {
                    File file = new File(fileName);
                    if (file.exists()) {
                        try {
                            resource = file.toURI().toURL();
                        } catch (MalformedURLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
        return resource;
    }
}

另外,您可以通过包含 maven 依赖项来使用我的 jk-util 项目,如下所示:

<dependency>
    <groupId>com.jalalkiswani</groupId>
    <artifactId>jk-util</artifactId>
    <version>0.0.9</version>
</dependency>

只需调用以下代码:

InputStream in = JKResourceLoaderFactory.getResourceLoader().getResourceAsStream("/config.xml");

最新更新