Accessing Resources in WAR/WEB-INF Folder
Accessing files within the war/WEB-INF folder in an app engine project can be achieved using the File() class. Here's how to construct the required path:
Java code:
import javax.servlet.ServletContext; ServletContext context = getContext(); String fullPath = context.getRealPath("/WEB-INF/test/foo.txt");
The above code utilizes the getRealPath() method of the ServletContext to obtain the full system path to the resource. Alternatively, you can use the following code if the servlet container doesn't expand the WAR file:
Java code:
import javax.servlet.ServletContext; ServletContext context = getContext(); URL resourceUrl = context.getResource("/WEB-INF/test/foo.txt");
Note that, alternatively, you can directly obtain the input stream using the getResourceAsStream() method:
import javax.servlet.ServletContext; ServletContext context = getContext(); InputStream resourceContent = context.getResourceAsStream("/WEB-INF/test/foo.txt");
Regardless of the servlet container or deployment location, the latter approach will always succeed. On the other hand, the former approach requires the WAR file to be unzipped prior to deployment.
The above is the detailed content of How to Access Resources in the WAR/WEB-INF Folder in an App Engine Project?. For more information, please follow other related articles on the PHP Chinese website!