このコードは、のために似た何かを作ります私(ただし、私のファイルは私のプロジェクトディレクトリの外にあります)。そのコントローラメソッドは、ファイル名(id)とファイルタイプ(filetype)を受け取り、あらかじめ定義されたディレクトリを探します。私はあなたの "serverLocation"と "contentLocation"を適合させる必要があると思います。ファイル配信する
List listOfNewsletters = []
String contentLocation = grailsApplication.config.managedNewsletter.contentLocation;
File rootDirectory = new File(servletContext.getRealPath("/"));
File webappsDirectory = rootDirectory.getParentFile();
String serverLocation = webappsDirectory.getAbsolutePath();
serverLocation = serverLocation + contentLocation + "/";
File f = new File(serverLocation);
if (f.exists()) {
f.eachFile() { File file ->
listOfNewsletters.push([
path: file.path,
filename: file.name,
filetype: "pdf"
])
}
}
:ビューに渡すことができるファイルのリストを取得するには
def openNewsletter() {
if (params.id != null && params.filetype != null) {
String pdf = (String) params.id + "." + (String) params.filetype;
String contentLocation = grailsApplication.config.managedNewsletter.contentLocation;
File rootDirectory = new File(servletContext.getRealPath("/"));
File webappsDirectory = rootDirectory.getParentFile();
String serverLocation = webappsDirectory.getAbsolutePath();
serverLocation = serverLocation + contentLocation + "/";
File pdfFile =new File(serverLocation + pdf);
response.contentType = 'application/pdf' // or whatever content type your resources are
response.outputStream << pdfFile.getBytes()
response.outputStream.flush()
}
}
申し訳ありませんが、実際には要求された解決策ではありません。しかし、とにかくあなたの注意に感謝します。 –