java getrealpath_JavaEE路径陷阱之getRealPath

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

如果你不想使用之前写过的助手类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方法。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369694.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

与客户患难与共

在採訪Werner Vogels(亚马逊的首席技术官)的过程中,他描写叙述了亚马逊的开发者是怎样跟他们的用户保持沟通的:记住。我们的大部分开发者都跟客户保持着良好的接触,因此他们对“客户喜欢什么”、“客户不喜欢什么”、“…

Camel 2.11 –没有Spring的Camel Web应用程序

在撰写本文时,我们刚刚向即将到来的Apache Camel 2.11添加了一个新组件,该组件允许使用Camel运行Web应用程序,而不必依赖Spring Framework来加速Camel。 或与此相关的任何其他第三方框架。 有点as愧地说,在2013年,我们…

关于盒模型的一点总结

标准w3c盒模型图示 关于怪异盒模型和标准盒模型的对比 关于怪异盒模型盒标准盒模型之间的转换

Day 9 函数的初识1

def my_len(): l1 [1,2,3,5,6] print(111) print(222) return print(333)print(my_len())一、函数的定义1.遇到return ,此函数结束,if 不在向下进行:2.return 的返回值 1. 不写return 返回为none 2. return non ,和1 …

java 反射机制性能_Java反射机制的用途和缺点

Java反射机制的用途和缺点反射的用途 Uses of ReflectionReflection is commonly used by programs which require the ability toexamine or modify theruntime behavior of applications running in the Java virtual machine. Thisis a relatively advancedfeature and shou…

最近的生活

这个暑假主要时间都在公司实习,没有去自己学习很新的一些技术,因为现在这个公司的技术栈不是特别的新,不过实习了一段时间发现,其实技术栈新不新和你一个新手根本就没有半毛钱关系,根基不牢学什么白搭是吧 举个例子&am…

将大型项目从Ant迁移到Maven

事实是,我们处在艰难时期。 我们花了将近三个月的时间将构建机制从Ant迁移到Maven 。 如果您打算在大型项目中做同样的事情,那就是您必须安排的最短时间。 我们仍在努力解决这种迁移带来的一些附带影响,但幸运的是,它们并不是那么…

移动端布局方案探究

研究了淘宝,天猫和网易彩票163的wap主页样式布局,总结移动端布局方案 注意:代码运行是file协议,在chrome里不支持引用本地文件,会提示跨域错误,可以用firefox或者Safari打开 当时做的ppt下载: 2015年12月移…

搭建springboot环境

1.前戏准备: SpringBoot核心jar包:这里直接从Spring官网下载了1.5.9版本. jdk:jdk1.8.0_45. maven项目管理工具:3.5版本. tomcat:8.5版本. 本地仓库:注意settings.xml里面的设置"<localRepository>E:/SpringBoot/repository</localRepository>"红色部分代…

java生成带文字的图片_Java在背景图片上面加文字或者文字自动生成图片

正巧遇到一个项目需要图片合成&#xff0c;查阅了网上许多资料之后整理了一份生成图片的工具类&#xff0c;代码如下&#xff1a;package org.yygzs.acker.util;import java.io.IOException;import javax.imageio.ImageIO;import java.io.File;import java.awt.Font;import jav…

style对象的cssText方法

cssText 本质是什么&#xff1f; cssText 的本质就是设置 HTML 元素的 style 属性值。 cssText 怎么用&#xff1f; domElement.style.cssText "color:red; font-size:13px;"; cssText 返回值是什么&#xff1f; 在某些浏览器中&#xff08;比如 Chrome&#xff09…

Spring的Web MVC –重定向到内存泄漏

他们说一块岩石会引起雪崩。 最近&#xff0c;我的一位同事Marcin Radoszewski给了我一块岩石。 您可能永远都不会猜测它是什么&#xff0c;但是有机会在许多Web应用程序中使用它。 请允许我向您介绍这块石头。 您可能很清楚发布模式后的重定向 。 使用Spring Framework&#…

Android 学习Kotlin吗?

学习Kotlin吗&#xff1f;转载于:https://www.cnblogs.com/zhujiabin/p/8398185.html

.Net Core 2.0 生态(1).NET Standard 2.0 特性介绍和使用指南

.NET Standard 2.0 发布日期&#xff1a;2017年8月14日公告原文地址 前言 早上起来.NET社区沸腾了&#xff0c;期待已久的.NET Core 2.0终于发布&#xff01;根据个人经验&#xff0c;微软的产品一般在2.0时会趋于成熟&#xff0c;所以一个新的.Net开发时代已经来临&#xff01…

jquery中的创建节点和添加节点的方法

今天来说一下针对jquary关于创建节点和添加节点的方法&#xff0c;先来看看创建节点&#xff1a; 1、直接添加节点内容即可 $("div").append(<a href"http://www.baidu.com">百度一下</a>); 2、可以创建jquary对象添加进去 var $li$(<a hre…

Java – 2012年回顾和未来预测

这篇文章将重点讨论2012年发生的大小事件&#xff0c;并展望2013年的一些未来预测。其中一些预测将是诚实的猜测&#xff0c;而另一些则将是诚实的猜测。 好吧&#xff0c;只要说我的“恶魔般”的一面就已经接管了。 因此&#xff0c;我们无需再讨论Java的2012年了。 2012年–…

移动端 fixed 固定按钮在屏幕下方,然后按钮被键盘顶上来...顶上来了有没有~

在移动端 H5 页面开发中&#xff0c;我使用了 fixed 固定某个元素在屏幕的最下方&#xff0c; 这时点击输入框&#xff0c;接着非常非常自然地出现了元素被系统键盘顶起来的情况&#xff0c;如下图。 解决方案: 首先&#xff0c;给页面最外层包裹一层 div(相对定位) &#xf…

momentum公式

momentum对于w的更新公式&#xff1a; http://caffe.berkeleyvision.org/tutorial/solver.html转载于:https://www.cnblogs.com/ymjyqsx/p/7364743.html

带弹簧的Hazelcast分布式执行

ExecutorService功能是Java 5附带的&#xff0c;位于java.util.concurrent包中。 它扩展了Executor接口&#xff0c;并提供了线程池功能来执行异步简短任务。 建议使用Java Executor服务类型来查看基本的ExecutorService实现。 此外&#xff0c;ThreadPoolExecutor是ExecutorS…

Manacher's Algorithm 马拉车算法(最长回文串)

这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法&#xff0c;由一个叫Manacher的人在1975年发明的&#xff0c;这个方法的最大贡献是在于将时间复杂度提升到了线性&#xff0c;这是非常了不起的。对于回文串想必大家都不陌生&#xff0c;就是…