我试图解组我的XML文件:如何从资源文件夹中获取文件。 Spring框架
public Object convertFromXMLToObject(String xmlfile) throws IOException {
FileInputStream is = null;
File file = new File(String.valueOf(this.getClass().getResource("xmlToParse/companies.xml")));
try {
is = new FileInputStream(file);
return getUnmarshaller().unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}
但我得到这个错误: java.io.FileNotFoundException:空(没有这样的文件或目录)
这里是我的结构:
为什么我无法从资源文件夹中的文件?谢谢。
更新。
重构后,
URL URL = this.getClass()的getResource( “/ xmlToParse/companies.xml”); File file = new File(url.getPath());
我能看得更清楚的错误:
java.io.FileNotFoundException:/content/ROOT.war/WEB-INF/classes/xmlToParse/companies.xml(没有这样的文件或目录)
尝试查找WEB-INF /班/ 我已经添加的文件夹存在,但仍获得此错误:(
尝试'的getResource(“类路径:xmlToParse/companies.xml”)' – sidgate
惯于你一个“/”前需要xmlToParse – LearningPhase
代码已更新。请检查出来。 –