从 Heroku 上播放的文件中读取 [英] Reading from a file in play on Heroku

查看:15
本文介绍了从 Heroku 上播放的文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 play 2.2 应用程序,它在/public"文件夹中包含一个名为books.json"的文件.在我的本地机器上,我可以成功地读取这个文件.但是,当我部署到 Heroku 时,我无法读取该文件.我收到以下错误消息:

I have a play 2.2 application that contains a file called "books.json" in the "/public" folder. On my local machine I can successfully read from this file. However, when I deploy to Heroku I cannot read from this file. I get the following error message:

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

我使用此代码从文件中读取:

I read from the file using this code:

    val booksDataPath = Play.getFile("/public/books.json").getAbsolutePath
    val json = new String(Files readAllBytes Paths.get(booksDataPath))

推荐答案

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

Use Play.resource instead. Everything in the public directory ends up on the classpath, so should be able to do:

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

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

在 Play 中依赖文件系统是不好的做法.

It's bad practice in Play to rely on the filesystem.

这篇关于从 Heroku 上播放的文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆