用溶液を得ました。出力ストリーム経由でブラウザからファイルを配信する必要があります。
2.)ウェブサイトのユーザーのディスクに自由にアクセスすることはできません。したがって、Webサイトが "c:\ website \ file.pdf"にファイルを作成した場合、Webサイトは常にサーバーの "c:"ドライブに移動し、エンドユーザー "c:"ドライブには移動しません。 「c:\」ドライブは主にWindowsのものなので、Unixやモバイルユーザは「c:」ドライブを持たないため、これを行うことはできます。
ので、ここでは
...あなたがファイルを格納し、ユーザーが(
テストし、それが良いかもしれなく、概念を示していない)今以降、それらをダウンロードできるようにしたい場合、私はどうなるのかです
class ReportController{
def runReport = {
File report = createReport() //<-- your method to create a report.
ReportDirectory.addFile(report);
redirect(action:"downloadFle" params:[fileName:report.name])
}
def showAllFiles = {
[files:ReportDirectory.listFiles()]
}
def downloadFile = {
def fileName = params.fileName;
def fileToDownload = ReportDirectory.findFile(fileName);
if (fileToDownload){
response.setContentType("application/octet-stream")
response.setHeader("Content-disposition", "attachment;filename=${fileToDownload .getName()}")
response.outputStream << fileToDownload.newInputStream() //<-- ask the user to download
}else{
//handle when the file is not found
}
}
def deleteAllFiles ={
ReportDirectory.deleteAllFiles()
[files:ReportDirectory.listFiles()] //<-- return the remaining files, if any.
}
}
....その後、あなたのコントローラで、あなたがこのようなことを行うことができ、レポート
class ReportDirectory{
static final String path = "./path/to/reports/"; //<-- generic path on your SERVER!
static{
//static initializer to make sure directory gets created. Better ways to do this but this will work!
File pathAsFile = new File(path).mkdirs()
if (pathAsFile.exists()){
println("CREATED REPORT DIRECTORY @ ${pathAsFile.absolutePath}");
}else{
println("FAILED TO CREATE REPORT DIRECTORY @ ${pathAsFile.absolutePath}");
}
}
public static File[] listFiles(){
return new File(path).listFiles(); //<-- maybe use filters to just pull pdfs?
}
public static void addFile(File file){
FilesUtil.copyFileToDirectory(file, new File(path)); //<-- using apache-commons-io
}
public static void deleteAll(){
listFiles().each(){ fileToDelete ->
fileToDelete.delete();
}
}
public static File findFile(String name){
listFiles().each(){ fileToCheck ->
if (fileToCheck.name.equals(name)){
return fileToCheck
}
}
return null
}
}
のディレクトリを表すクラスを作成しますこの解決策に関するいくつかのコメント...
- これはMIMEタイプに対応していないため、ブラウザはどのようなバイナリデータがどのような種類のバイナリデータであるかを知ることができません。
この情報は役に立ちましたか?
ジャスパーレポート – n92