2012-02-20 9 views
2

私は現在、私のLiftプロジェクトのwebappフォルダ内に画像を保存しています。Scala Lift - アップロードしたファイルをサーバのディレクトリに保存します

val path = "src/main/webapp/files/" 

そして、私はそれを保存するために使用しているコード:

case Full(file) => 

    val holder = new File(path, "test.txt") 
    val output = new FileOutputStream(holder)    

    try { 

     output.write(file) 

    } finally { 

     output.close() 

    } 

} 

私が何をしようとしているが、/ファイルと呼ばれる簡単に管理フォルダ内のサーバーのルートに保存し、そうSERVER_ROOTですプロジェクトフォルダ外のファイル。

まず、サーバーのルートへのパスにアクセスすると、そこに保存できますか?

第2に、これらのファイルをアプリケーションからどのように提供すれば、ページに表示できるのですか?事前に

おかげで、任意の助けに感謝:)

答えて

2

あなたは絶対パスに応じて、ファイルシステム上の場所を強要するために、ファイルを保存する必要があります。

def storeFile (file : FileParamHolder): Box[File] = 
    { 
    getBaseApplicationPath match 
     { 
      case Full(appBasePath) => 
      { 
       var uploadDir = new File(appBasePath + "RELATIVE PATH TO YOUR UPLOAD DIR") 
       val uploadingFile = new File(uploadDir, file.fileName) 

       println("upload file to: " + uploadingFile.getAbsolutePath) 

       var output = new FileOutputStream(uploadingFile) 
       try 
       { 
        output.write(file.file) 
       } 
       catch 
       { 
        case e => println(e) 
       } 
       finally 
       { 
        output.close 
        output = null 
       } 

       Full(uploadingFile) 
      } 
      case _ => Empty 
     } 
    } 

、これは、ローカルマシン(サーバーまたはお使いのdevel PC)の絶対パスを見つけ出し、私のgetBaseApplicationPath機能である:私はこのコードを書かれているし、それが動作しますので、多分それはあなたを助けます

def getBaseApplicationPath: Box[String] = 
    { 
     LiftRules.context match 
     { 
      case context: HTTPServletContext => 
      { 
       var baseApp: String = context.ctx.getRealPath("/") 

       if(!baseApp.endsWith(File.separator)) 
        baseApp = baseApp + File.separator 

       Full(baseApp) 
      } 
      case _ => Empty 
     } 
    } 
+0

助けていただきありがとうございます。あなたの既存のコードでgetApplicationPath関数をどのように使用しますか?たとえば、ファイルを "C:/ files /"に保存したいとします。再度、感謝します – jhdevuk

関連する問題