我有一个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))
答案 0 :(得分:5)
请改用Play.resource
。 public
目录中的所有内容都以类路径结束,因此应该能够执行:
Play.resource("public/books.json")
或
Play.resourceAsStream("public/books.json")
在Play中依赖文件系统是不好的做法。