我试图读取放置在WEB-INF根路径中的配置文件。 该应用程序正在使用RESTLET框架。 我使用Context.getClientDispatcher()在官方RESTLET 文档中读取了它可能通过WAR连接器(“war:///WEB-INF/web.xml”)读取文件。
但是我无法弄清楚如何实现这一点。 请让我知道这个或任何其他方式可以使用RESTLET读取文件
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().
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才能访问配置文本文件。 这里是代码示例 -
// 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);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);更多推荐
发布评论