`

关于serveletContext.getRealPath()方法

阅读更多

1.关于request.getRealPath 问题: String filename=request.getRealPath(filename) ------------------- 信息: warning: [deprecation] getRealPath(java.lang.String) in javax.servlet.ServletRequest has been deprecated 解决:这个getRealPath方法已经不建议使用了 参看request.getRealPath的java doc: Deprecated. As of Version 2.1 of the Java Servlet API, use ServletContext.getRealPath(java.lang.String) instead. 而在servlet中使用getServletContext.getRealPath()这个方法受到war 和non-war的影响,以及不同app server实现的影响,运气好的话,你常常会得到null,嘿嘿,比如你在weblogic上部署war文件,又调用这个方法.. 推荐ServletContext.getResourceAsStream

2.关于serveletContext.getRealPath返回NULL和不同的app server返回不同的结果 问题: 有几个配置文本配置文件(是一些报表的模板),放在WEB-INF下面的config目录下,程序中是这样得到这个config的实际路径的: 先用 serveletContext.getRealPath得到根路径,tomcat中比如是 c:\tomcat\webapp\test 然后我加上 "/WEB-INF/config/aa.config",这样得到文件的path然后进行读入,应用在tomcat上跑是ok的,后来将war放到weblogic上,出错,原因是:在weblogic上用getRealPath得到的是像 myserver\stage\_appsdir_test_war\test.war!\WEB-INF\config.... 这样的路径,于是一直报FileNotFoundException 解决: serveletContext.getRealPath 这个方法在不同的服务器上所获得的实现是不一样的,建议是通过classloader来获得你配置的资源文件 context.getRealPath("/")可能返回了null,你可以输入来看看, 对一个打包的应用来说,是没有RealPath的概念的,调用getRealPath只会简单地返回null。其实,也很 好理解,一个文件被打包入了.war文件,就不存在目录结构了(虽然包中仍然存在目录结构,但这不等同于文件系统中的目录结构)。所以,对war包中的资源是无法得到RealPath的。这样也就无从通过文件IO进行读取了。 那么,如何读取war包中的资源呢?答案是使用: ServletContext.getResourceAsStream("/WEB-INF/config/aa.config")方法。 原则:基本上就是尽量使用j2ee规范中的各层次classloader来获取资源,而不是试图去找文件的绝对路径方法:调用this.getClass().getClassLoader().getResource("/").getPath(); 获取到classes目录的全路径 使用:在得到classes目录的全路径后再根据字符串的截取与拼装达到你的要求即可。 绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!应该使用ServletContext的getResource()方法,直接使用相对于Web应用根目录的相对路径来获取资源。 ServletContext接口中定位资源的方法 getResource java.net.URL getResource(java.lang.String path) throws java.net.MalformedURLException Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root. This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file. The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource. This method returns null if no resource is mapped to the pathname. Some containers may allow writing to the URL returned by this method using the methods of the URL class. The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution. This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders. Parameters: path - a String specifying the path to the resource Returns: the resource located at the named path, or null if there is no resource at that path Throws: java.net.MalformedURLException - if the pathname is not given in the correct form -------------------------------------------------------------------------------- getResourceAsStream java.io.InputStream getResourceAsStream(java.lang.String path) Returns the resource located at the named path as an InputStream object. The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource. This method returns null if no resource exists at the specified path. Meta-information such as content length and content type that is available via getResource method is lost when using this method. The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource. This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader. Parameters: path - a String specifying the path to the resource Returns: the InputStream returned to the servlet, or null if no resource exists at the specified path getRealPath java.lang.String getRealPath(java.lang.String path) Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.. The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive). Parameters: path - a String specifying a virtual path Returns: a String specifying the real path, or null if the translation cannot be performed 说明可以看到,ServletContext接口中的getResource()等方法,可以找到任何从应用程序的根目录开始的资源。包括在.war包这样的压缩文件中。参数必须以/开头。而我们常用的getRealPath(“/”)方法,在.war包发布时,就会失效。会返回null。因此,我们应该避免使用getRealPath(“/”)这样的方法来获取应用程序的绝对路径。

分享到:
评论

相关推荐

    Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别.doc

    其中`getContextPath()`, `getServletPath()`, `getRequestURI()`以及`getRealPath()`是四个常用方法,它们分别用于获取不同的路径信息。理解这些方法的作用及区别有助于更好地进行Web应用的开发与维护。 #### 二、...

    Struts Updownload 源码

    String realPath=ServletActionContext.getServletContext().getRealPath("/"+this.getPath()+"/"+fileName); System.out.println(realPath); return ServletActionContext.getServletContext()....

    java绝对路径和相对路径

    在Web应用开发中,推荐使用`request.getRealPath`或`ServletContext`的`getRealPath`方法来获取绝对路径;而在一般的Java应用中,则可以使用`System`类或类加载器来获取本地路径。对于相对路径,可以根据当前目录...

    javaweb 做图片水印,水印图片到目录图片上去

    添加水印方法(水印图片,目标图片),添加在右下角(根据坐标显示) 针对网页图片添加水印,用java语言编写,很简单。 // 获取水印图片的路径 String planeImage = request.getSession().getServletContext()....

    java获取路径的各种方法

    总结来说,Java中获取路径涉及相对路径和绝对路径的概念,以及多种获取实际路径的方法,如`HttpServletRequest`的`getRealPath()`、`System.getProperty()`和`ServletContext`等。理解和熟练运用这些方法对于开发...

    jsp中获得路径的两种方法和获得url路径的方法(推荐).docx

    然而,`request.getRealPath("/")`方法虽然可以获取到服务器上JSP文件的实际物理路径,如`d:\web\`,但这个方法在现代的Java Web应用中已经不再推荐使用,因为它依赖于服务器的文件系统,这在分布式和容器化的环境中...

    基于springboot的便利店信息管理系统 源码+数据库(毕业设计)

    基于Vue.js和SpringBoot的便利店信息管理系统是一个高效、易用的解决方案,旨在为便利店的日常运营提供全面支持。该系统分为用户前台和管理后台两个部分,分别满足不同用户角色的需求。管理员和员工均可通过该系统...

    JAVA获取各种路径总结

    - 方法:`new File(application.getRealPath(request.getRequestURI())).getParent()` - 如果你需要获取某个文件所在目录的上级目录,可以先通过`application.getRealPath(request.getRequestURI())`得到该文件的...

    java开发中的路径问题

    正确理解和使用这些路径处理方法对于Java Web开发至关重要,因为它们直接影响到资源的定位、文件操作以及网络请求的正确性。开发者应尽量避免使用像`.`, `./`, `../`这样的相对路径,以免因文件移动导致问题。在可能...

    struts2+spring2+ibates

    取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent() servlet类似! 6、在写action类里,对于要封装的值和对象,除了常数以外,记得SET和GET,不是会出现空指针。

    Java相对路径与肯定路径的问题_.docx

    1. **Servlet中的`getRealPath()`**: 在Servlet中,可以使用`HttpServletRequest`或`ServletContext`的`getRealPath()`方法来获取绝对路径。例如: ```java String path = request.getServletContext()....

    JSP中java文件操作大全

    首先,我们需要使用`request.getRealPath("")`方法来获取服务器上的实际路径,然后通过这个路径创建`File`对象。例如,要创建或检查`File.txt`文件是否存在,代码如下: ```java String path = request.getRealPath...

    java读写文件,Java操作文件

    上述代码展示了如何通过`request.getRealPath()`方法来获取不同级别的目录路径。这对于动态构建文件路径非常有用。 #### 文件属性的取得 获取文件属性也是Java文件操作中的重要部分,可以帮助我们更好地管理文件...

    在JAVA文件中获取该项目的相对路径.doc

    在 Servlet 中,可以使用 request.getServletPath() 获取当前文件的绝对路径,然后使用 request.getSession().getServletContext().getRealPath(request.getRequestURI()) 获取当前文件的绝对路径。同时,也可以使用...

    JAVA文件路径详细讲解[文].pdf

    在Servlet中的`request.getRequestDispatcher()`和`response.sendRedirect()`方法中,路径是服务器端解析的,所以应以Web应用为基准。 - **客户端地址**:HTML页面和JavaScript中的相对路径是相对于服务器的根目录...

    获取路径的各种方法

    1. `request.getRealPath("/")`:这个方法返回的是Web应用的根目录在服务器上的物理路径。例如,如果Web应用名为"strutsTest",路径可能是"C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\...

    Java获取*路径实现探讨

    //获取jsp的路径,这个方法比较好用,可以直接在servlet和jsp中使用 (3)、request.getSession().getServletContext().getRealPath(“/”);//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用 (4)...

    得到文件真实路径

    - 请求文件的上层目录:`new File(application.getRealPath(request.getRequestURI())).getParent()` 5. **在Servlet中获取路径** - 根目录的绝对路径:`request.getServletPath()` - 文件的绝对路径:`request....

    getServletContext()空指针异常的原因

    getServletContext()空指针异常的原因getServletContext()空指针异常的原因getServletContext()空指针异常的原因getServletContext()空指针异常的原因getServletContext()空指针异常的原因

Global site tag (gtag.js) - Google Analytics