我正在尝试解组我的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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:java.io.FileNotFoundException:null(没有这样的文件或目录)
这是我的结构:
为什么我无法从资源文件夹中获取文件?谢谢.
更新.
重构后,
URL url = this.getClass().getResource("/ xmlToParse/companies.xml"); 文件文件=新文件(url.getPath());
我可以更清楚地看到错误:
java.io.FileNotFoundException:/content/ROOT.war/WEB-INF/classes/xmlToParse/companies.xml(没有这样的文件或目录)
它试图找到WEB-INF/classes /我在那里添加了文件夹,但仍然收到此错误:(
是否有一种简单的方法可以将Flow配置为从类路径中读取单个文件一次?我不需要轮询文件.我只需要读取一个已知文件并将其内容设置为消息有效负载.
如何使用spring表达式读取文件内容并将其放入字符串?
我想做以下几点.
例如,
@Value("classpath:myquery.sql")
File f;
@Value("#{org.apache.commons.io.FileUtils.readFileToString(f)}")
String sql;
Run Code Online (Sandbox Code Playgroud)
甚至更好
@Value("#{FileUtils.readFileToString(classpath:myquery.sql)}")
String sql;
Run Code Online (Sandbox Code Playgroud)
但是,上述代码都不起作用.
只是提到我使用的是Spring 3.2.0版
谢谢.