在生产中的播放框架中从公共文件夹中读取文件

use*_*107 8 playframework playframework-2.4

我的播放应用程序是2.4.2.在开发人员模式中,我曾经使用Source.fromFile("./ public/files/abc.json")从后端控制器的公用文件夹中读取文件

当我将应用程序转换为生产模式时,我收到了File Not Found Exceptions.我发现公用文件夹在生产模式下打包在资产jar中.我能做什么才能在开发和生产模式下工作?

Sup*_*hne 7

您是否尝试过此 Play 文档https://www.playframework.com/documentation/2.5.x/AssetsOverview?这对 Play 2.4 来说没问题,即使是 2.3,我也试过了。

在那里你可以找到这个,你可以简单地在你的 conf/routes 文件中做到这一点。

GET  /assets/*file        controllers.Assets.at(path="/public", file)
Run Code Online (Sandbox Code Playgroud)

file -> 指的是文件名。例如:myFile.json

要在生产模式下完成这项工作,您必须做更多的工作。如答案所述,将这些行添加到您的 /build.sbt 文件中。

import com.typesafe.sbt.packager.MappingsHelper._
    mappings in Universal ++= directory(baseDirectory.value / "public")
Run Code Online (Sandbox Code Playgroud)

这将在 dist 文件中包含您的“公共”目录(您可以像这样包含任何目录)。然后您的应用程序将在生产环境中工作。