how to read a file from server in play framework

前端 未结 4 2070
悲哀的现实
悲哀的现实 2021-02-07 13:16

I have the following file

/app/menus/menu1.yml

and I\'d like to read it\'s contents

--

short answer:

fileConten         


        
4条回答
  •  梦毁少年i
    2021-02-07 13:58

    From Play 2.6 this is now in Environment. And I suggest using either .getExistingFile which returns an option in case file does not exist. Or .resource which returns a URL to anything in the classpath only.

    https://www.playframework.com/documentation/2.6.x/api/scala/index.html#play.api.Environment

     class Someclass @Inject (environment: play.api.Environment) {
     // ...
    
     environment.getExistingFile("data/data.xml").fold{
       // NO FILE. PANIC
     }{ file =>
       // Do something magic with file
     }
    

提交回复
热议问题