我想转换smooks xml-java ,以便我需要从中加载源文件
mobeeadmin.war/WEB-INF/sample.xml
。
Smooks smooks = new Smooks("/WEB-INF/sample.xml");
抛出以下异常:
java.io.IOException: Failed to access data stream for resource [/WEB-INF/sample.xml]. Tried (in order):
10:10:14,113 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) File System: E:\WEB-INF\sample.xml
10:10:14,114 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) File System: E:\jboss-as-7.1.1.Final\bin\WEB-INF\sample.xml
10:10:14,117 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) Classpath: /WEB-INF/sample.xml
10:10:14,125 ERROR [stderr] (http-localhost-127.0.0.1-8080-2)
By default it looks in File System: E:\jboss-as-7.1.1.Final\bin\WEB-INF\sample.xml .I want load from E:\jboss-as-7.1.1.Final\standalone\deployments\myproject.war\WEB-INF\sample.xml.
答案 0 :(得分:0)
E:/data/sample.xml
应该可以正常工作。ServletContext.getResourceAsStream(java.lang.String path)
获取流加载它。由于JBoss7是Servlet 3.0编译器,您可以从HttpServletRequest获取ServletContext。然而,一些现代的框架不会给你。如果您想从类路径获取文件,可以将其移至WEB-INF / classes并通过类加载器加载。但是,对于正确的java来说,java非常挑剔。最可靠的是ContextClassloader(它现在位于根包中):
Thread.currentThread().getContextClassLoader().getResourceAsStream("sample.xml");