【问题标题】:What does the path get from the server by getRealPath () in jspjsp中getRealPath()从服务器获取的路径是什么
【发布时间】:2012-02-01 05:59:29
【问题描述】:

我正在使用 struts 1.3,jsp 来开发应用程序。 我想知道下面的代码会从服务器返回什么路径。

 path = getServlet().getServletContext().getRealPath("/") +"images\\logos\\"+ formFile.getFileName();

服务器的路径是什么。我可以使用此路径在页面上显示图像吗?

【问题讨论】:

    标签: jsp jakarta-ee servlets struts relative-path


    【解决方案1】:

    首先:不推荐使用 getRealPath。 (比较:Interface ServletRequest)。你应该试试这个(从规范 2.1 开始):

    ServletContext context = session.getServletContext();
    String realContextPath = context.getRealPath(request.getContextPath());
    

    在此之前,它高度依赖于服务器实现。根据规范,如果将应用程序部署为存档模块(war、ear 等),则允许返回 null。我相信这从未发生过,例如与 WebLogic。它返回解压档案的临时目录的路径。所以,为了简短的回答:

    你的代码最终会是这样的:

    x:\your\path\on\drive\images\logos\somename.ext
    

    不能将其用作图片网址。

    【讨论】:

    • OP 没有使用ServletRequest#getRealPath()。
    猜你喜欢
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多