我有一个 play 2.2 应用程序,它在“/public”文件夹中包含一个名为“books.json”的文件。在我的本地机器上,我可以成功地读取这个文件。但是,当我部署到 Heroku 时,我无法读取该文件。我收到以下错误消息:
2013-09-30T08:12:11.027238+00:00 app[web.1]: [error] application -
2013-09-30T08:12:11.027238+00:00 app[web.1]:
2013-09-30T08:12:11.027238+00:00 app[web.1]: ! @6fmoh6ol7 - Internal server error, for (GET) [/bookReviews] ->
2013-09-30T08:12:11.027238+00:00 app[web.1]:
2013-09-30T08:12:11.027238+00:00 app[web.1]: play.api.Application$$anon$1: Execution exception[[NoSuchFileException: /app/target/universal/stage/public/books.json]]
2013-09-30T08:12:11.027238+00:00 app[web.1]: at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$12$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:165) ~[com.typesafe.play.play_2.10-2.2.0.jar:2.2.0]
2013-09-30T08:12:11.027238+00:00 app[web.1]: at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$12$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:162) ~[com.typesafe.play.play_2.10-2.2.0.jar:2.2.0]
2013-09-30T08:12:11.027405+00:00 app[web.1]: at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:185) ~[org.scala-lang.scala-library-2.10.2.jar:na]
2013-09-30T08:12:11.027405+00:00 app[web.1]: at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33) ~[org.scala-lang.scala-library-2.10.2.jar:na]
2013-09-30T08:12:11.027405+00:00 app[web.1]: at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) ~[na:1.7.0_25]
2013-09-30T08:12:11.027405+00:00 app[web.1]: at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55) ~[na:1.7.0_25]
2013-09-30T08:12:11.027405+00:00 app[web.1]: at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) ~[na:1.7.0_25]
2013-09-30T08:12:11.027238+00:00 app[web.1]: at play.api.DefaultApplication.handleError(Application.scala:399) ~[com.typesafe.play.play_2.10-2.2.0.jar:2.2.0]
2013-09-30T08:12:11.027238+00:00 app[web.1]: at play.api.Application$class.handleError(Application.scala:293) ~[com.typesafe.play.play_2.10-2.2.0.jar:2.2.0]
2013-09-30T08:12:11.027405+00:00 app[web.1]: at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) ~[na:1.7.0_25]
2013-09-30T08:12:11.027405+00:00 app[web.1]: Caused by: java.nio.file.NoSuchFileException: /app/target/universal/stage/public/books.json
2013-09-30T08:12:11.027405+00:00 app[web.1]: at sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:96) ~[na:1.7.0_25]
2013-09-30T08:12:11.027405+00:00 app[web.1]: at sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:143) ~[na:1.7.0_25]
2013-09-30T08:12:11.047069+00:00 heroku[router]: at=info method=GET path=/bookReviews host=ntcodingplay.herokuapp.com fwd="81.138.22.60" dyno=web.1 connect=1ms service=566ms status=500 bytes=1941
我使用以下代码从文件中读取:
val booksDataPath = Play.getFile("/public/books.json").getAbsolutePath
val json = new String(Files readAllBytes Paths.get(booksDataPath))