在我的Web应用程序中,我有以下代码:
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的工作方式和如何解决的区别?
答案 0 :(得分:0)
好的,通过注意到Tomcat6没有在末尾添加“ /”,而Tomcat8却在其末尾解决了问题。所以我的新代码修复了它: LOGGER.info(“为客户端获取套接字:” + 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”目录),所以这就是我们的方法。不希望更改任何内容,只需添加该部分代码以使其起作用。
我在网上看到了其他解决方法。