how to read a file from a lift webapp

[亡魂溺海] 提交于 2019-12-09 18:29:27

问题


I want to read xml file in my lift app:

val data = XML.load(new java.io.InputStreamReader(new java.io.FileInputStream(filename), encoding));

However, I am getting java.io.FileNotFoundException. where should I put the file, and what is the correct path from my scala code?

BTW - I am using embedded jetty for my testing, although I need a solution for dev env and production.


回答1:


There might be better solution for other paths but there is

var LiftRules.getResource : (String) ⇒ Box[URL]

or

def LiftRules.doWithResource(name: String)(f: (InputStream) ⇒ T): Box[T]

which usually point to files in src/main/resources/.

There is also

def LiftRules.loadResourceAsXml(name: String): Box[NodeSeq]

which may be the method you are looking for.




回答2:


To solve this problem in the general case, new File("delme").createNewFile() and see where it ends up.



来源:https://stackoverflow.com/questions/7546887/how-to-read-a-file-from-a-lift-webapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!