ファイルが残っているかディレクトリが存在しません。 Spring Application Contextを作成するGroovyスクリプト内で実行しています。同じ種類のパスを使用して別のファイルを簡単に読み込みます。しかし、私が読んでいるファイルはSpringのクラスパスにあります。このスクリプトは、さまざまなファイルシステムを持つ多数のユーザーによって実行される可能性があるため、パスをハードコードすることはできません。私は相対的な道が必要です。新しいファイルを任意のディレクトリに保存するGroovy
これはクラスの上にありますが、重要な情報です。
private static String saveFilesToLocation = "/retrieve/";
ここにコードがあります。
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
counter++
if (nextLine != null && (nextLine[0] != 'FileLocation')) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length());
File file = new File(saveFilesToLocation+fileLocation);
file.withOutputStream {
it.write fileBytes
}
println "$counter) Wrote file ${fileLocation} to ${saveFilesToLocation+fileLocation}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
文字列内のパスには、余分な文字は含まれません。
UPDATE:
おかげであなたの答えは、あまりにも作業し、働いていた私たちの最終的な結果よりも優れgroovinessを持っているでしょうloteq。それは一種のものなので、あなたが持っているより良いバージョンに変更する時間がありません。
私たちのために働いたコードはここにありますが、saveFilesToLocationがすでに存在するディレクトリに設定されている点を除いて、これは上記と同じです。以前のものは存在しなかったので、loteq のようにmkdirを呼び出す必要がありました。
private static String saveFilesToLocation = "/tmp/retrieve/";
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null && (nextLine[0] != 'FileLocation')) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length());
File file = new File(saveFilesToLocation+fileName);
file.withOutputStream {
it.write fileBytes
}
println "$counter) Wrote file ${fileLocation} to ${saveFilesToLocation+fileLocation}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
} else {
counter++;
}
}