navid
navid

Reputation: 95

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

I have this code on my project with play 2.5 in spec2 and it's worked fine. Recently I updated play 2.6.9 and now it does not work anymore.

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

I tried changing it to:

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]

It compiles but in runtime can't convert java.io.file to a temporary file. The error is:

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

Upvotes: 2

Views: 2720

Answers (2)

Majid Hosseini
Majid Hosseini

Reputation: 1096

You can replace

val tempFile = file.asInstanceOf[TemporaryFile]

with

val tempFile = SingletonTemporaryFileCreator.create(file.toPath)

and so you can use TemporaryFileCreator too, for more info and another use you can see this Interface Files.TemporaryFile

Upvotes: 3

Ali
Ali

Reputation: 267077

Looks like you have to use TemporaryFileCreator now to create it: https://www.playframework.com/documentation/2.6.9/api/java/play/libs/Files.TemporaryFile.html

Upvotes: 0

Related Questions