如何在 Play 2.6.x 中将 java.io.File 转换为 Scala 临时文件

How to convert java.io.File to Scala Temporary File in Play 2.6.x

我的项目中有这段代码,在 spec2 中有 play 2.5,它运行良好。 最近我更新了 play 2.6.9,现在它不再工作了。

val file = new File(s"$home/the.upload.file4.$fileName")
val pw = new PrintWriter(file)
pw.write(fileContent)
pw.close
val tempFile = TemporaryFile(file)

我尝试将其更改为:

val file = new File(s"$home/the.upload.file4.$fileName")
val pw = new PrintWriter(file)
pw.write(fileContent)
pw.close
val tempFile = file.asInstanceOf[TemporaryFile]

它可以编译,但在运行时无法将 java.io.file 转换为临时文件。错误是:

[error] java.lang.ClassCastException: java.io.File cannot be cast to play.api.libs.Files$TemporaryFile

看来您现在必须使用 TemporaryFileCreator 来创建它:https://www.playframework.com/documentation/2.6.9/api/java/play/libs/Files.TemporaryFile.html

你可以替换

val tempFile = file.asInstanceOf[TemporaryFile]

val tempFile = SingletonTemporaryFileCreator.create(file.toPath)

因此您也可以使用 TemporaryFileCreator, 有关更多信息和其他用途,您可以看到这个 Interface Files.TemporaryFile