Home>

I want to create an API to get a file using playframework × scala, but I want to specify a temporary file as an intermediate file in the directory where play is running, but I don't know how. There was a lot of old information on the net (play 2.0 and so on), so it wasn't helpful.
Does anyone know a solution?

Current code
// play.api.libs.Files.TemporaryFile version
// controller
import play.api.libs.Files.TemporaryFile
import play.api.libs.Files.SingletonTemporaryFileCreator
...
..
def test = Action {implicit request =>
  val file = SingletonTemporaryFileCreator.create (prefix = "hoge", suffix = ".tmp")
  println (file.path)
  ...
  ..
}
// console
// C: \ Users \ USB16A ~ 1.ER \ AppData \ Local \ Temp \ hoge6983486703041487604.tmp


If i use createTempFile of java.io.File, you can specify the directory, but you cannot use the setting (application.conf) to delete it automatically.

// java.io.File version
// controller
import java.io.File
...
..
  val file = File.createTempFile (prefix = "hoge", "/ test/txt")
  println (file.path)
// console
// test \ txt \ hoge6983486703041487604.tmp
environment

play: 2.8
sbt: 1.1.4
scala: 2.13.2
jdk: 11

  • Answer # 1

    I have not confirmed the operation, but if you look at the API document, it seems that you can usually create an instance from the constructor.
    new TemporaryFile (file: File)
    https://www.playframework.com/documentation/2.0/api/scala/play/api/libs/Files $$TemporaryFile.html

    The way to take the path is the same as Java, so it seems to be helpful as follows.
    https://qiita.com/ka2kama/items/9e16cc6d1019838770cc