因此,我正在开发一个使用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
发布于 2016-06-10 04:19:53
在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");https://stackoverflow.com/questions/37735073
复制相似问题