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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4340653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:09:53  来源:igfitidea点击:

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

javagoogle-app-engineweb-applications

提问by user291701

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 文件夹中有一个文件。我在常见问题解答中读到,您可以在 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

谢谢

采纳答案by Berin Loritsch

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

这将为您提供您正在寻找的资源的完整系统路径。但是,如果 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 contextfield. In a servlet you get it from your ServletConfigwhich 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 页面使其可用作context字段。在 servlet 中,您从ServletConfig传递到 servlet 的init()方法中获取它。如果您在那个时候存储它,那么您可以在此之后随时获取您的 ServletContext。

回答by Daniel De León

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

回答by whiz

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");