2015-05-07 74 views
1

我正在使用scala playframework。我正在上傳文件。在服務器端,我正在成功接收文件。我想從它創建一個File對象。怎麼做?scala/play - 從上傳的文件製作一個File對象

這是我迄今爲止編寫的代碼。

request.body.file("dataFile").map(
    currentFile => { 
     // How to make a FILE object from currentFile 
     //Something like 
     //val file: File = new File(currentFile) 
    } 
) 

這可能嗎?如果是的話那麼如何?提前致謝。

回答

1

按照documentation應該以這種方式工作:

request.body.file("dataFile").map(
    currentFile => { 
     // How to make a FILE object from currentFile 
     import java.io.File 
     val filename = currentFile.filename 
     val contentType = currentFile.contentType 
     val myFile = new File(s"/tmp/somepath/$filename") 
     currentFile.ref.moveTo(myFile) 
     //now "myFile" is an object of type "File". 
    } 
) 
+0

謝謝您的回答。什麼是「/ tmp/somepath /」?我可以寫「somepath」還是應該特定於應用程序? –

+0

我收到編譯錯誤。 –

+0

只需輸入您要存儲文件的路徑。它可以是絕對的(即,根據您的操作系統,可以是'c:\\ myFiles \\ test.txt'或'/ myFiles/test.txt')或相對(您的項目目錄)。如果你使用'somepath.ext',它會在你的項目根目錄下創建一個文件。 – Peanut

相關問題