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

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

问题描述

我有一个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 - 内部服务器错误,用于(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:执行异常[[NoSuchFileException:/app/target/universal/stage/public/books.json]]
2013-09-30T08:12:11.027238 + 00:00 app [web .1]:在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.sca la-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应用[web.1]:由于: 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.60dyno = web.1 connect = 1ms service = 566ms status = 500 bytes = 1941

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

  val booksDataPath = Play。 getFile(/ public / books.json)。getAbsolutePath 
val json = new String(文件readAllBytes Paths.get(booksDataPath))


解决方案使用 Play.resource 代替。所有 public 目录中的所有内容都在类路径中结束,因此应该可以这样做:

  Play.resource(public / books.json)

  Play.resourceAsStream(public / books.json)

Play中依赖文件系统是个不好的习惯。


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))

解决方案

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")

or

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

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

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

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