gpt4 book ai didi

scala - 从 Heroku 上正在 Play 的文件中读取

转载 作者:行者123 更新时间:2023-12-02 04:49:43 26 4
gpt4 key购买 nike

我有一个 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))

最佳答案

改用Play.resourcepublic 目录中的所有内容都在类路径中,因此应该能够:

Play.resource("public/books.json")

Play.resourceAsStream("public/books.json")

在 Play 中依赖文件系统是一种不好的做法。

关于scala - 从 Heroku 上正在 Play 的文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19089412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com