问题描述
我们在Cloudbees上运行Play2应用程序,并从"/conf"目录(在应用程序的类路径中)加载文件.
We run Play2 application on Cloudbees and we load a file from '/conf' directory (inside the classpath of the application).
这2个代码段可在本地和heroku中使用
These 2 snippets work in local and at heroku
Play.application().getFile("conf/myfile.json")
和
new File("conf/myfile.json")
但是,在Cloudbees上,我们得到FileNotFoundException:
java.io.FileNotFoundException: /var/genapp/apps/..../conf/myfile.json (No such file or directory)
那么如何从Cloudbees的类路径中加载文件?
So how to load a file from classpath on Cloudbees?
推荐答案
好吧,'/conf'中的文件位于类路径中,而不位于文件系统中,因此我们需要以这种方式加载文件:
Well, files in '/conf' are in the classpath and not on the filesystem so we need to load the file this way :
Play.application.resourceAsStream("myfile.json")
//.resource() also works - depends what we want
请注意,我们不要在路径中放入"conf",其中的文件位于根目录的类路径中.
请注意,在生产中,它来自jar/zip文件,而不是文件-因此getFile在播放时会产生误导.
Note that in production it comes from a jar/zip, not a file - so getFile is somewhat misleading in play.
Cloudbees的Michael Neale提出了这个问题: https://github.com/playframework/Play20/issues/1079
Michael Neale from Cloudbees opened this issue : https://github.com/playframework/Play20/issues/1079
Cloudbees文档已更新: https://wiki.cloudbees.com/bin/view/RUN /Playframework#HLoadingconfigfilesinproduction
Cloudbees documentation has been updated : https://wiki.cloudbees.com/bin/view/RUN/Playframework#HLoadingconfigfilesinproduction
这篇关于从Cloudbees的'/conf'目录加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!