问题描述
我正在尝试读取位于WEB-INF根路径内的配置文件.该应用程序正在使用RESTLET框架.我阅读了官方的RESTLET doc ,可以使用Context.getClientDispatcher()通过WAR连接器("war:///WEB-INF/web.xml")读取文件.
I'm trying to read a config file placed inside the root path of WEB-INF. The application is using RESTLET framework. I read in the official RESTLET doc that its possible to read files via a WAR connector ("war:///WEB-INF/web.xml") using Context.getClientDispatcher().
但是我无法弄清楚该如何实现.请告知我有关此信息或使用RESTLET读取文件的任何其他方式
However i was not able to figure out as to how this can be achieved. Kindly let me know about this or any other ways a file can be read using RESTLET
推荐答案
找到了解决方案.我必须访问ServletContext才能进入配置文本文件.这是代码示例-
Found the solution for this. I had to access the ServletContext to get to the configuration text file. Here is the code sample -
// Extract the ServletContext from the attributes of RestletContext
ServletContext servlet = (ServletContext) context.getAttributes().get("org.restlet.ext.servlet.ServletContext");
// Get the path of the config file relative to the WAR
String rootPath = servlet.getRealPath("/WEB-INF/configuration.txt");
Path path = Paths.get(rootPath);
File configFile = new File(path.toString());
FileRepresentation file = new FileRepresentation(configFile, MediaType.TEXT_PLAIN);
这篇关于在RESTLET中读取Web-INF中的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!