我正在尝试读取放置在 WEB-INF 根路径内的配置文件。该应用程序使用RESTLET框架。我在官方RESTLET上读到doc http://restlet.com/technical-resources/restlet-framework/javadocs/snapshot/gae/ext/org/restlet/ext/servlet/ServerServlet.html可以使用 Context.getClientDispatcher() 通过 WAR 连接器(“war:///WEB-INF/web.xml”)读取文件。
但是我无法弄清楚如何实现这一点。请让我知道这个或使用 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)