【发布时间】:2016-07-24 06:50:40
【问题描述】:
我正在尝试解组我的 xml 文件:
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: null (没有这样的文件或目录)
这是我的结构:
为什么我无法从资源文件夹中获取文件?谢谢。
更新。
重构后,
URL url = this.getClass().getResource("/xmlToParse/companies.xml"); 文件 file = new File(url.getPath());
我可以更清楚地看到错误:
java.io.FileNotFoundException: /content/ROOT.war/WEB-INF/classes/xmlToParse/companies.xml(没有这样的文件或目录)
它试图找到 WEB-INF/classes/ 我在那里添加了文件夹,但仍然收到此错误:(
【问题讨论】:
-
试试
getResource("classpath:xmlToParse/companies.xml") -
在 xmlToParse 之前是否需要另一个“/”
-
代码已更新。请检查一下。
标签: java spring-mvc java-io fileinputstream