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(“/”)这样的方法来获取应用程序的绝对路径。
相关推荐
windows linux 下,获取java项目绝对路径的方法,struts2设置了struts.multipart.saveDir后会在根目录建立文件夹,这样会涉及linux下的权限问题
android获取文件绝对路径类,是本人项目中直接取出的类。在protected void onActivityResult()中使用
java project和java web project项目中如何获得相对路径、绝对路径问题详解
本文档是本人经过多次实验和长时间的累计总结的java路径问题
QT文件夹定位 QT文件夹绝对路径 QT文件夹绝对路径定位 QT文件夹定位 QT文件夹绝对路径 QT文件夹绝对路径定位 QT文件夹定位 QT文件夹绝对路径 QT文件夹绝对路径定位
开发web工程时经常要获取工程的根目录,自己用Java实现的获取Tomcat下war包部署的Web工程根目录路径的方法,主要利用web工程默认的目录结构,此外也可以指定工程名称获取工程目录的绝对路径
下面小编就为大家分享一篇python3中获取文件当前绝对路径的两种方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
一文讲清Python PyQt5的控件如何实现拖放获取文件路径 内部以QLineEdit为例子,从新类创建,代码理解,到融入项目均详细讲解。
http://blog.csdn.net/love_xiolan/article/details/54945859 博文地址 此代码,下载后,倒入到eclipse中即可使用..
尽量使用URI形式的绝对路径资源。它可以很容易的转变为URI,URL,File对象。尽量使用相对classpath的相对路径。不要使用绝对路径。使用上面 ClassLoaderUtil类的public static URL getExtendResource(String relative...
在项目开发过程中,在部署过程中,用到了反向代理,这就要求前端代码中不能使用绝对路径。但是我们知道,一般情况下,通过webpack+vuecli默认打包后的HTML、css、js等文件,使用的都是绝对路径。下面可以举几个例子...
在项目中,我们经常要得到项目的绝对路径,方便我们上传下载文件,JS为我们提供了方法,虽说要迂回一下。代码如下: function getRealPath(){ //获取当前网址,如: http://localhost:8083/myproj/view/my.jsp ...
获取项目的相对路径,根据绝对的路径计算"\\"个数,然后转成相对路径 注意大家下载后在 string path11 = "dddd"; 之后加一句 int i=one.Length;
在 运行,调试,打包成exe 三个不同场景下获取跟路径,用于解决获取资源文件绝对路径问题。 工具类代码如下: import sys import os class pathutil(object): 路径处理工具类 def __init__(self): # 判断调试...
yarn-config-目录 获取全局纱线配置目录的绝对路径。 请考虑关注该项目的作者 ,并考虑为该项目以显示您的 :red_heart: 和支持。安装使用安装:$ npm install --save yarn-config-directory用法路径因平台而异。 ...
主要给大家介绍了关于vue cli使用绝对路径引用图片问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。
绝对路径的概念 整个文件的完整路径,比如X:\www\web\index.html,或者 http://waldo.com.cn/index.html 。这两者都属于绝对路径。 相对路径多种使用方式详解 假设当前有一个页面,要链接到一个页面名称为test.html...
//=> 'foo'相关项目其他有用的node.js路径实用程序: :如果文件路径为绝对路径,则返回true。 | :如果路径看起来是相对的,则返回true 。 | :将文件路径解析为对象。 在本机node.js的回落path.parse方法,如果......
在项目中需要用到将Uri转换为绝对路径,在网上找到一个方法,做个笔记 网上有不少方法,但是有的对4.4后的版本无效,这里的方法可以在4.4之后的版本将Uri转换为绝对路径 public class GetPathFromUri { /** * 专...
这是开头 最近在试水 Vue CLI 3,并且尝试配置一个多页面(多应用)项目出来,期间又遇到各种路径... 通过绝对路径被引用,即以 / 开头的路径。 2、以下情况下,资源会被 webpack 处理(URL的resolve、minify、uglif