首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在空位置找不到文件config.xml :无协议: config.xml -如何设置config.xml的路径

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

Stack Overflow用户
提问于 2016-06-10 03:55:24
回答 1查看 963关注 0票数 0

因此,我正在开发一个使用HTML、CSS和AngularJS的基本webapp,并且我已经开始使用Spring添加功能。当我添加了几个JAR时,我开始收到错误消息DEBUG DefaultFileSystem - Could not locate file config.xml at null: no protocol: config.xml It‘t find my config.xml document,这个文档当前位于src/main/resources,但我已经尝试过其他地址。我可以在哪里设置存放文件的路径,而不是将其设置为null

EN

回答 1

Stack Overflow用户

发布于 2016-06-10 04:19:53

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

代码语言:javascript
复制
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项目,如下所示:

代码语言:javascript
复制
<dependency>
    <groupId>com.jalalkiswani</groupId>
    <artifactId>jk-util</artifactId>
    <version>0.0.9</version>
</dependency>

只需调用以下代码:

代码语言:javascript
复制
InputStream in = JKResourceLoaderFactory.getResourceLoader().getResourceAsStream("/config.xml");
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37735073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档