【发布时间】:2019-03-07 20:27:54
【问题描述】:
在我的网络应用程序中,我有以下代码:
if( context == null )
throw new WMSException( "Missing session context." );
String path = context.getRealPath("");
if( path == null )
throw new WMSException( "Missing context real path." );
WebSocket ws = new WebSocket();
String sep = "/";
int where = path.lastIndexOf( sep );
if( where < 0 ){
sep = "\\";
where = path.lastIndexOf( sep );
}
path = path.substring( 0, where );
where = path.lastIndexOf( sep );
path = path.substring( 0, where ) + "/conf/wms";
if( firstTime ){
firstTime = false;
System.out.println( "Getting configuration from " + path );
}
File docFile = new File(path, "socket.xml");
System.out.println( "Name " + docFile.getName() );
System.out.println( "Path " + docFile.getPath() );
在 tomcat 6 中,由于 getRealPath 的工作方式,我得到以下信息:
/usr/share/tomcat6/conf/wms/socket.xml
在 tomcat 8 中,对于同一个 war 文件,我得到以下信息:
/opt/tomcat8/webapps/conf/wms/socket.xml
为什么 getRealPath 的工作方式存在差异,我该如何解决?
【问题讨论】:
-
GetRealPath 应该在你的 webapp 中获取路径。 Tomcat6 版本没有这样做,它正在返回指向您的 Tomcat 安装中某些内容的路径。所以他们修复了 Tomcat 8 中的错误。
-
getRealPath用于获取war文件中文件的路径,即Tomcat提取文件的暂存区域。该位置在哪里完全没有记录,您永远不应该使用该路径访问war文件内容之外的文件。您需要修复您的代码,以便通过其他方式告诉它在哪里可以找到外部文件。 -
那么我应该调用什么来代替 getRealPath,它与 Tomcat6 对 getRealPath 所做的相同?