2012-02-20 6 views
2

현재 내 리프트 프로젝트의 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() 

    } 

} 

가 난 할 노력하고있어하는 파일이라는 관리가 용이 ​​한 폴더에 서버 루트에 저장, 그래서 SERVER_ROOT/프로젝트 폴더 외부의 파일

첫 번째로 서버의 루트 경로에 어떻게 접근하여 거기에 저장할 수 있습니까?

두 번째로 내 앱에서이 파일을 어떻게 제공하여 페이지에 표시 할 수 있습니까? 사전에

덕분에, 어떤 도움이 많이 감사합니다 :)

답변

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

을이 절대 로컬 컴퓨터의 경로 (서버 또는 (STABLE) 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

관련 문제