"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Published on 2024-11-13
Browse:647

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3