简体   繁体   English

我们的 war/WEB-INF 文件夹中资源的文件路径?

[英]File path to resource in our war/WEB-INF folder?

I've got a file in my war/WEB-INF folder of my app engine project.我的应用引擎项目的 war/WEB-INF 文件夹中有一个文件。 I read in the FAQs that you can read a file from there in a servlet context.我在常见问题解答中读到,您可以在 servlet 上下文中从那里读取文件。 I don't know how to form the path to the resource though:我不知道如何形成资源的路径:

/war/WEB-INF/test/foo.txt

How would I construct my path to that resource to use with File(), just as it looks above?我将如何构建该资源的路径以与 File() 一起使用,就像上面看到的那样?

Thanks谢谢

There's a couple ways of doing this.有几种方法可以做到这一点。 As long as the WAR file is expanded (a set of files instead of one .war file), you can use this API:只要 WAR 文件被扩展(一组文件而不是一个 .war 文件),你就可以使用这个 API:

ServletContext context = getContext();
String fullPath = context.getRealPath("/WEB-INF/test/foo.txt");

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getRealPath(java.lang.String) http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getRealPath(java.lang.String)

That will get you the full system path to the resource you are looking for.这将为您提供您正在寻找的资源的完整系统路径。 However, that won't work if the Servlet Container never expands the WAR file (like Tomcat).但是,如果 Servlet 容器从不扩展 WAR 文件(如 Tomcat),这将不起作用。 What will work is using the ServletContext's getResource methods.可以使用 ServletContext 的getResource方法。

ServletContext context = getContext();
URL resourceUrl = context.getResource("/WEB-INF/test/foo.txt");

or alternatively if you just want the input stream:或者,如果您只想要输入流:

InputStream resourceContent = context.getResourceAsStream("/WEB-INF/test/foo.txt");

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getResource(java.lang.String) http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getResource(java.lang.String)

The latter approach will work no matter what Servlet Container you use and where the application is installed.无论您使用什么 Servlet 容器以及应用程序安装在哪里,后一种方法都可以使用。 The former approach will only work if the WAR file is unzipped before deployment.前一种方法只有在部署前解压缩 WAR 文件时才有效。

EDIT: The getContext() method is obviously something you would have to implement.编辑: getContext() 方法显然是你必须实现的东西。 JSP pages make it available as the context field. JSP 页面使其可用作context字段。 In a servlet you get it from your ServletConfig which is passed into the servlet's init() method.在 servlet 中,您从ServletConfig获取它,该ServletConfig传递给 servlet 的init()方法。 If you store it at that time, you can get your ServletContext any time you want after that.如果您在那个时候存储它,那么您可以在此之后随时获取您的 ServletContext。

Now with Java EE 7 you can find the resource more easily with现在使用 Java EE 7,您可以更轻松地找到资源

InputStream resource = getServletContext().getResourceAsStream("/WEB-INF/my.json");

https://docs.oracle.com/javaee/7/api/javax/servlet/GenericServlet.html#getServletContext-- https://docs.oracle.com/javaee/7/api/javax/servlet/GenericServlet.html#getServletContext--

I know this is late, but this is how I normally do it,我知道这已经晚了,但这就是我通常的做法,

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();           
InputStream stream = classLoader.getResourceAsStream("../test/foo.txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM