Simon Simon - 2 months ago 20
Scala Question

Is it possible to access a file outside a Play! application folder?

I would like to let two Play applications( deployed on the same server) to access a same folder, but I don't find the way to do it.

Is it possible to access a file by its absolute path, so I could put this folder at the directory level of my server and access it by the two applications?

Answer

Yes it is possible to access any folder given correct permissions to the play app process. You could access any file in the filesystem using Java file.

Ensure read write permissions accordingly if not Play throws FileNotFoundException

import java.io.File

@Singleton
class ApplicationController @Inject() () extends Controller {
 def file = Action {
  Ok.sendFile(new File(s"${sys.props("user.home")}/some_file.txt"))
 }
}

In the above example some_file.txt is send over http connection. Note that if permission are not correct then it will throw FileNotFoundException

Comments