Google App Engine (GAE) では、次のようないくつかの方法を使用して、展開された WAR 内の任意のファイルを読み取ることができることを理解しています。
String file = "/WEB-INF/name-of-my-filexml";
InputStream in = getClass().getResourceAsStream(file);
問題は、次のようなディレクトリ構造で Web アプリをデプロイする必要があることです。
MyApp/
WEB-INF/
lib/
classes/
web.xml
appengine-web.xml
...
profiles/
fizz.txt
buzz.txt
foo.txt
... dozens of other text files
各profiles/*.txt
ファイルを Java 文字列に読み込む方法が必要です。そして、誰かがコメントする前に、なぜ文字列をハードコーディングしないのか...、簡単な質問を投稿するために、ここで多くのバックストーリーを切り取っているとだけ言っておきましょう。冗談で、文字列をハードコーディングできないふりをしましょう。通常、 へのフル アクセスがあれば、java.io.*
次のようにします。
File profilesHome = new File("path/to/profiles");
File[] profiles = profilesHome.listFiles();
List<String> profileList = new ArrayList<String>();
for(File profile : profiles)
profileList.add(readFileIntoString(profile));
しかし、ここでは、 を呼び出すことができないと思いますFile#listFiles()
。また、InputStream
から返されるしかない場合、それをハンドルまたは String オブジェクトgetClass().getResourceAsStream(file)
に変換する方法がわかりません。File
何か案は?前もって感謝します。
更新:ZipInputStream
提案を使用:
InputStream inputStream = event.getServletContext()
.getResourceAsStream("/WEB-INF/profiles.zip");
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
List<String> list = new ArrayList<String>();
ZipEntry currEntry;
try {
while((currEntry = zipInputStream.getNextEntry()) != null)
// TODO: How to convert the contents of currEntry to a string
// in a manner that is GAE-friendly?
list.add(convertEntryContentsToString(currEntry));
} catch (IOException e) {
e.printStackTrace();
}
さて、どのように実装しconvertEntryContentsToString(ZipEntry)
ますか?