如何在静态Web应用程序中指定相对文件路径 [英] How do I specify relative file paths in restful web applications

查看:105
本文介绍了如何在静态Web应用程序中指定相对文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宁静的应用程序的部署将从一个WAR文件中分解".所有文件路径都必须是相对的,而不是绝对的(从单个WEB-INF目录嵌套).指定这些文件(例如.properties文件.xml文件等)的惯例是什么?

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

错误指定示例:file f = file(/usr/home/username/.../WEB-INF/file.something);

我要实现的示例:(来自Controller/Servlet)file f = new file(file.something);

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

推荐答案

绝对不要使用/usr/home或任何绝对路径指定文件.正确的方法是使用 ServletContext.getRealPath()

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

还有 getResourceAsStream 查看全文

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