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

File Scala Lift-将上载的文件保存到服务器目录,file,scala,lift,File,Scala,Lift,我目前正在我的Lift项目的webapp文件夹中存储图像,我知道这将在将来引起问题 val path = "src/main/webapp/files/" 我用来保存它的代码是: case Full(file) => val holder = new File(path, "test.txt") val output = new FileOutputStream(holder) try { output.wr

我目前正在我的Lift项目的webapp文件夹中存储图像,我知道这将在将来引起问题

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() 

    }

}
我要做的是将文件保存到一个名为files的易于管理的文件夹中的服务器根目录中,以便服务器根目录/项目文件夹之外的文件

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

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


提前感谢,非常感谢您的帮助:)

您必须根据绝对路径将文件存储到文件系统上的确切位置。我已经编写了这段代码,它很有效,所以它可能会帮助您:

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)的绝对路径:


感谢您的帮助,我将如何在现有代码中使用getApplicationPath函数?例如,我想将文件保存到“C:/files/”。再次感谢
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
        }
    }