有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    你可能正在寻找^{}

    Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "". The container does not decode this string.

    。。。或^{}

    Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.

  2. # 2 楼答案

    你对HttpServletRequest^{}方法感兴趣

  3. # 3 楼答案

    I need to use it using JSP or Servlet to get a file path.

    那么文件被存储在战争的公共网络内容中了?使用^{}

    String relativeWebPath = "/file.jpg";
    String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
    File file = new File(absoluteDiskPath);
    InputStream input = new FileInputStream(file); // I guess this is what you want.
    // ...
    

    请注意,这仅在容器扩展战争时有效。否则,如果你真正想要的只是得到^{,最好使用^{}

    String relativeWebPath = "/file.jpg";
    InputStream input = getServletContext().getResourceAsStream(relativeWebPath);
    // ...
    

    另请参见: