Scala Lift - 将上传的文件保存到服务器目录

问题描述:

我目前将图像存储在我的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 /项目文件夹外的文件。

首先,我将如何访问服务器根目录的路径,以便将它们保存在那里?

其次,我将如何从我的应用程序提供这些文件,以便我可以在页面上显示它们?提前

谢谢,任何帮助非常感谢:)

你要存储的文件根据绝对路径文件系统上的确切地点。我写了这个代码和它的作品,所以也许它可以帮助你:

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 
     } 
    } 

,这是我getBaseApplicationPath功能,发现本地计算机的绝对路径(服务器或devel的PC):

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 2012-02-29 11:01:34