How do I specify relative file paths in restful web applications

拟墨画扇 提交于 2019-12-12 01:51:52

问题


Deployment of my restful application will be "exploded" from a single WAR file. All file paths will need to be relative and not absolute (Nesting from a single WEB-INF directory). What is the convention for specifying these files (such as .properties files .xml files etc)?

Example of wrongly specifying: file f = file(/usr/home/username/.../WEB-INF/file.something);

Example of what I'm trying to achieve: (From Controller/Servlet) file f = new file(file.something);


回答1:


You should NEVER specify a file using /usr/home or any absolute path. The correct method is using ServletContext.getRealPath()

context.getRealPath("/WEB-INF/file.something");

There is also getResourceAsStream and getResource.



来源:https://stackoverflow.com/questions/6708412/how-do-i-specify-relative-file-paths-in-restful-web-applications

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