Home >Java >javaTutorial >How to Access Files in the War/WEB-INF Folder in App Engine?

How to Access Files in the War/WEB-INF Folder in App Engine?

DDD
DDDOriginal
2024-11-13 09:54:02995browse

How to Access Files in the War/WEB-INF Folder in App Engine?

Accessing Files in War/WEB-INF Folder in App Engine

Reading files within the war/WEB-INF folder in an App Engine project involves constructing a suitable path to the resource. To do this, you have two options:

Option 1: ServletContext's getRealPath() Method

This approach works if the WAR file is expanded (a set of files instead of a single .war file).

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

Option 2: ServletContext's getResource Method

This approach always works, regardless of whether the WAR file is expanded or not.

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

Alternatively, to obtain the input stream directly:

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

You can obtain the ServletContext from a JSP page via the context field or from a servlet via the ServletConfig that is passed into the servlet's init() method.

The above is the detailed content of How to Access Files in the War/WEB-INF Folder in App Engine?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn