【问题标题】:getRealPath between Tomcat6 and Tomcat8Tomcat6 和 Tomcat8 之间的 getRealPath
【发布时间】: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 所做的相同?

标签: java tomcat8 tomcat6


【解决方案1】:

好的,通过注意到Tomcat6没有在末尾添加“/”但Tomcat8有,问题解决了。所以我的新代码修复了它: LOGGER.info("获取客户端的socket:" + 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”目录),所以我们就是这样做的。不想改变任何事情,只是添加了那段代码以使其工作。

网上看到的还有其他解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 2023-03-18
    • 2012-07-09
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多