Tomcat6 和 Tomcat8 之间的 getRealPath
getRealPath between Tomcat6 and Tomcat8
在我的网络应用程序中,我有以下代码:
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 的工作方式不同以及我该如何解决?
好的,注意 Tomcat6 没有在末尾放置一个“/”但 Tomcat8 有,问题就解决了。所以我的新代码修复了它:
LOGGER.info("Get socket for client: " + 客户);
if( context == null )
throw new WMSException( "Missing session context." );
String path = context.getRealPath("");
if( path == null )
throw new WMSException( "Missing context real path." );
LOGGER.info( "Path 1 '" + path + "'");
WebSocket ws = new WebSocket();
String sep = "/";
int where = path.lastIndexOf( sep );
if( where < 0 ){
sep = "\";
where = path.lastIndexOf( sep );
}
String newPath = path.substring( 0, where );
if( newPath.equals(path.substring(0, path.length() - 1 )))
{
where = newPath.lastIndexOf( sep );
path = newPath.substring( 0, where );
LOGGER.info( "Path 2a '" + path + "'");
}
else
path = newPath;
LOGGER.info( "Path 2 '" + path + "'");
where = path.lastIndexOf( sep );
path = path.substring( 0, where ) + "/conf/wms";
LOGGER.info( "Path 3 '" + path + "'");
if( firstTime ){
firstTime = false;
LOGGER.info( "Getting configuration from " + path );
}
LOGGER.info( "Path 4 '" + path + "'");
File docFile = new File(path, "socket.xml");
LOGGER.info( "Name '" + docFile.getName() + "';Path '" + docFile.getPath() + "'");
关键是 if( newPath.equals(path.substring(0, path.length() - 1 )))
if 部分。显然,它可以通过其他方式解决。另外网上很多地方都暗示有这个问题,我只是漏接了路径末尾的“/”,所以没能正确上目录树。
获取 tomcat 主目录(我们有 "conf" 目录)并不容易,所以我们就是这样做的。不想更改内容,只是添加了那段代码以使其工作。
网上看到还有其他的解决方法
在我的网络应用程序中,我有以下代码:
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 的工作方式不同以及我该如何解决?
好的,注意 Tomcat6 没有在末尾放置一个“/”但 Tomcat8 有,问题就解决了。所以我的新代码修复了它: LOGGER.info("Get socket for client: " + 客户);
if( context == null )
throw new WMSException( "Missing session context." );
String path = context.getRealPath("");
if( path == null )
throw new WMSException( "Missing context real path." );
LOGGER.info( "Path 1 '" + path + "'");
WebSocket ws = new WebSocket();
String sep = "/";
int where = path.lastIndexOf( sep );
if( where < 0 ){
sep = "\";
where = path.lastIndexOf( sep );
}
String newPath = path.substring( 0, where );
if( newPath.equals(path.substring(0, path.length() - 1 )))
{
where = newPath.lastIndexOf( sep );
path = newPath.substring( 0, where );
LOGGER.info( "Path 2a '" + path + "'");
}
else
path = newPath;
LOGGER.info( "Path 2 '" + path + "'");
where = path.lastIndexOf( sep );
path = path.substring( 0, where ) + "/conf/wms";
LOGGER.info( "Path 3 '" + path + "'");
if( firstTime ){
firstTime = false;
LOGGER.info( "Getting configuration from " + path );
}
LOGGER.info( "Path 4 '" + path + "'");
File docFile = new File(path, "socket.xml");
LOGGER.info( "Name '" + docFile.getName() + "';Path '" + docFile.getPath() + "'");
关键是 if( newPath.equals(path.substring(0, path.length() - 1 )))
if 部分。显然,它可以通过其他方式解决。另外网上很多地方都暗示有这个问题,我只是漏接了路径末尾的“/”,所以没能正确上目录树。
获取 tomcat 主目录(我们有 "conf" 目录)并不容易,所以我们就是这样做的。不想更改内容,只是添加了那段代码以使其工作。
网上看到还有其他的解决方法