java getrealpath_JavaEE路径陷阱之getRealPath「建议收藏」

java getrealpath_JavaEE路径陷阱之getRealPath「建议收藏」JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!提供这个方法绝对是JavaEEAPI开发组的一大败笔。使用它,我们会万劫不复!绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!应该使用ServletContex…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!提供这个方法绝对是JavaEE API开发组的一大败笔。使用它,我们会万劫不复!

绝对不要使用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(“/”)这样的方法来获取应用程序的绝对路径。

如果你不想使用我在《Java路径问题最终解决方案—可定位所有资源的相对路径寻址》中提出的助手类ClassLoaderUtil 的“public static URL getExtendResource(String relativePath)”方法,那么你应该使用ServletContext接口的“java.net.URL getResource(java.lang.String path) throws java.net.MalformedURLException”方法,URL对象可以方便的转为URI,和String对象。尽管没有ServletContext的源码,但是我可以猜想到getResource等方法一定在底层使用了ClassLoader的getResource方法。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191695.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux 进程抓包命令,linux抓包命令之tcpdump详解[通俗易懂]

    linux 进程抓包命令,linux抓包命令之tcpdump详解[通俗易懂]顾名思义,tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息,tcpdump凭借强大的功能和灵活的截取策略,使其成为类UNIX系统下用于网络分析和问题排查的首选工具.实用命令实例:(1).默认启动#普通情况下,直接启动tcpdump将监视第一个网络接口上所有流过的数据包.[[…

    2022年6月17日
  • MVC过滤器的详细讲解和示范样本

    MVC过滤器的详细讲解和示范样本

    2022年1月14日
  • 3分钟搞定下载微信视频号视频!无需第三方软件,亲测有效!

    3分钟搞定下载微信视频号视频!无需第三方软件,亲测有效!2020年是视频号的元年,现在2021视频号还处在发展初期,但是它的潜力是巨大的,将来的价值会超过抖音。你在视频号上点赞的视频,你的好友都会看到,这一点非常有利于营销推广。但抖音上的粉丝和微信联系人是割裂的,我们所有的社交关系都在微信上。但是视频号的短视频内容无法像抖音、快手一样,保存本地或者复制作品链接进行解析下载。有没有其他小技巧能绕过视频号未完善的功能,直接保存视频内容呢?当然有,请记住一句话,在android的系统中,视频是所见即所得本文只针对android系统,不需要借助任何第三方软件,

    2022年6月17日
  • 实验室设备管理系统c语言程序设计_设备管理系统怎么实现

    实验室设备管理系统c语言程序设计_设备管理系统怎么实现#include#include#includetypedefstructequipment{intnum;//编号charname[20];//名称charmodel[20];//型号charfactory[20];//厂家charbuy_date[20];//购买日期struc

    2022年10月13日
  • 无名的ADRC的C语言实现

    无名的ADRC的C语言实现分为ADRC.h和ADRC.c#ifndef_ADRC_H_#define_ADRC_H_/*———————————————————————————————————————-/*本程序只供购买者学习使用,版权著作权属于无名科创团队,*无名科…

    2022年5月19日
  • SSM 整合详细步骤

    SSM 整合详细步骤SSM整合第一章环境准备1、创建数据库和表结构createdatabasessm;createtableaccount(idintprimarykeyauto_increment,namevarchar(100),moneydouble(7,2),);2、创建Maven工程创建ssm_parent父工程(打包方式选择pom,必须的)创建ssm_web子模块(打包方式是war包)创建ssm_service子模块(打包方式是jar包)创建ssm_dao子模

    2022年5月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号