我在App Engine上有一个文件位于/WEB-INF/config.txt
,请问在App Engine上该文件的路径是什么?
例如: new File(/*我应该放什么路径在这里?*/)
我在App Engine上有一个文件位于/WEB-INF/config.txt
,请问在App Engine上该文件的路径是什么?
例如: new File(/*我应该放什么路径在这里?*/)
在我这个案例中,我没有访问 ServletContext - getServletContext() 的权限:
下面这种方法对我有用:
InputStream inputStream = new FileInputStream(new File("WEB-INF/config.txt"));
在appengine的文档中有关于如何向项目中添加资源的说明,请查看<resource-files>部分,具体内容请参考:如何向项目中添加资源。更多信息可以在appengine沙盒的描述中了解到。
当资源被添加到项目中后,您可以使用以下代码来读取它们的内容:
File f = new File("path/below/my/project/directory/config.txt");
InputStream in = new FileInputStream(f);
import java.net.URL;
import java.io.File;
import java.io.FileInputStream;
import com.google.common.io.CharStreams;
URL resource = getServletContext().getResource("/WEB-INF/config.txt");
File file = new File(resource.toURI());
FileInputStream input = new FileInputStream(file);
String myString = CharStreams.toString(new InputStreamReader(input, "UTF-8"));
src/main/resources
目录下:appengine-web.xml
中添加以下内容,授予对该文件夹的访问权限:<resource-files>
<include path="/resources" />
</resource-files>
URL resource = EmailSender.class.getClassLoader().getResource("email-template.html");
if (resource != null) {
File tempFile = new File(resource.getFile());
if (tempFile.exists()) {
htmlMessage = FileUtils.readFileToString(tempFile, StandardCharsets.UTF_8);
}
}