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

时间:2022-08-19 17:18:26

I've got a file in my war/WEB-INF folder of my app engine project. I read in the FAQs that you can read a file from there in a servlet context. I don't know how to form the path to the resource though:

我的应用程序引擎项目的war / WEB-INF文件夹中有一个文件。我在FAQ中读到你可以从servlet上下文中读取文件。我不知道如何形成资源的路径:

/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

谢谢

2 个解决方案

#1


128  

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). What will work is using the ServletContext's getResource methods.

这将为您提供所需资源的完整系统路径。但是,如果Servlet容器从不扩展WAR文件(如Tomcat),那将无法工作。什么工作是使用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. The former approach will only work if the WAR file is unzipped before deployment.

无论您使用什么Servlet容器以及安装应用程序的位置,后一种方法都可以工作。只有在部署之前解压缩WAR文件时,前一种方法才有效。

EDIT: The getContext() method is obviously something you would have to implement. JSP pages make it available as the context field. In a servlet you get it from your ServletConfig which is passed into the servlet's init() method. If you store it at that time, you can get your ServletContext any time you want after that.

编辑:getContext()方法显然是你必须实现的东西。 JSP页面使其可用作上下文字段。在servlet中,您可以从ServletConfig中获取它,并将其传递给servlet的init()方法。如果您在那时存储它,您可以在此之后随时获取ServletContext。

#2


1  

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--

#1


128  

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). What will work is using the ServletContext's getResource methods.

这将为您提供所需资源的完整系统路径。但是,如果Servlet容器从不扩展WAR文件(如Tomcat),那将无法工作。什么工作是使用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. The former approach will only work if the WAR file is unzipped before deployment.

无论您使用什么Servlet容器以及安装应用程序的位置,后一种方法都可以工作。只有在部署之前解压缩WAR文件时,前一种方法才有效。

EDIT: The getContext() method is obviously something you would have to implement. JSP pages make it available as the context field. In a servlet you get it from your ServletConfig which is passed into the servlet's init() method. If you store it at that time, you can get your ServletContext any time you want after that.

编辑:getContext()方法显然是你必须实现的东西。 JSP页面使其可用作上下文字段。在servlet中,您可以从ServletConfig中获取它,并将其传递给servlet的init()方法。如果您在那时存储它,您可以在此之后随时获取ServletContext。

#2


1  

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--