如何在 SessionListener 中获取 JSF 中的真实路径
我的要求如下:
信息:icefaces上传组件,将文件上传到相关文件夹,并为每个用户在该文件夹中使用sessionId创建一个子目录。
要求:在每个用户的sessionDestroyed
处,我想获取真实路径,以删除当前用户文件夹。
我知道如何使用 JSF 获取真实路径,如下所示:
ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
.getExternalContext().getContext();
String deploymentDirectoryPath = ctx.getRealPath("/");
问题: 如果您尝试在 sessionDestroyed
中获取真实路径,您将得到空指针异常,所以我想知道如果有一种方法可以在侦听器中初始化变量 deploymentDirectoryPath
,以便我可以在 sessionDestroyed
方法中使用它,或者可以在应用程序启动时初始化真实路径变量并使用它 这里 ?
请告知如何解决这个问题。
my requirement is as follows:
INFO: icefaces upload component, uploads the files to relative folder and creates for each user a sub-directory in that folder with the sessionId.
Requirement: at the sessionDestroyed
for each user, i want to get the real path, to delete current user folder.
i know how to get the real path with JSF as follows:
ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
.getExternalContext().getContext();
String deploymentDirectoryPath = ctx.getRealPath("/");
Problem: if you try to get the real path in sessionDestroyed
you will get null pointer exception, so i was wondering if there's a way to initialize the variable deploymentDirectoryPath
in the listener so i can use it in sessionDestroyed
method, or maybe initialize the real path variable on application startup and use it here ?
please advise how to solve this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即使您还没有发布与问题相关的实际代码,以下内容也给了我真正的路径:
输出
Even though you haven't posted the actual code that relates to the problem, the following gives me the real path:
Output
根据这里和其他地方的 BalusC 合理建议,我们可以编写一个如下所示的通用函数:
Based on BalusC sound advice here and elsewhere one could write a general purpose function like this:
您可以按如下方式获取真实路径:-
<strong>编辑:-
正如 BalusC 在他的一个答案中提到的,在使用
getRealPath("/")
之前您应该三思而后行,因为如果没有选择扩展 war 文件,则getRealPath("/")
可能会返回null
。请改用 getExternalContext.getResourceAsStream 。根据,docs,有效在应用程序启动或关闭期间调用此方法。
You can get the real path as follows:-
EDIT:-
As mentioned by BalusC in one of his answers, you should think twice before using
getRealPath("/")
because if one has not chose to expand the war file then,getRealPath("/")
might returnnull
.Use
getExternalContext.getResourceAsStream
instead. as per, docs, It is valid to call this method during application startup or shutdown.