java 为什么 ServletContext#getRealPath("/") 返回一个相对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1406605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:26:15  来源:igfitidea点击:

Why does ServletContext#getRealPath("/") return a relative path?

javaservletsjakarta-ee

提问by Jan Hoeve

I have the following snippet of code:

我有以下代码片段:

String path = servletContext.getRealPath("/");

Now I got a bugreport from a user saying that the returned path is not an absolute path. The returned path is 'usr/local/...' instead of '/usr/local/...', so getRealPathseems to be returning a relative path.

现在我收到一个用户的错误报告,说返回的路径不是绝对路径。返回的路径是 ' usr/local/...' 而不是 ' /usr/local/...',因此getRealPath似乎返回的是相对路径。

I can see this, because the returned path is logged into a logfile.

我可以看到这一点,因为返回的路径已记录到日志文件中。

My specs are:

我的规格是:

  • JBoss 4.0.5.GA
  • Redhat EL 4
  • jdk 1.5.0
  • JBoss 4.0.5.GA
  • 红帽 EL 4
  • JDK 1.5.0

See herefor the javadoc

有关javadoc,请参见此处

回答by Jan

Something like

就像是

String path = new File(servletContext.getRealPath("/")).getAbsolutePath();

should solve your problem. (It does not answer your Question though...;-) )

应该可以解决您的问题。(虽然它没有回答你的问题......;-))

Regards, Jan

问候, 简