`
dy3168729
  • 浏览: 39637 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

getRealPath

    博客分类:
  • J2EE
阅读更多

1.关于request.getRealPath的出错
问题:
String filename  =  request.getRealPath(filename);  //出了错
-------------------
错误信息:
The method getRealPath(String) from the type ServletRequest is deprecated

解决:
资料来源:http://bbs.chinaunix.net/viewthread.php?tid=383861

参看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

参考资料:http://bbs.chinaunix.net/viewthread.php?tid=383861

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目录的全路径后再根据字符串的截取与拼装达到你的要求即可。


同时WebLogic对WEB-INF目录下的文件是有访问控制的,最好不要放在WEB-INF目录下。

-------------------------------------------------------------------------

 

 

实际项目中,一个使用serveletContext.getRealPath访问WebRoot下config文件夹里的xml配置文件,确实可行,但是操作有些讲究,我是把WebRoot(即编译需要部署的根目录)移动到C盘下,将WebRoot改名为项目context名,然后部署,不能部署eclipse项目的原因尚不清楚~~~先记下来 ,以后研究~!

分享到:
评论

相关推荐

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

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

    GetRealPath.java

    用于添加其他应用列表打开文件获取微信QQ 分享过来的文件真实路径,并替换路径中存在的编码格式问题,解决文件上传媒体库找不到传输的文件,可以在微信,QQ中直接选择自己的应用打开,并获取到文件的路径!

    Struts Updownload 源码

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

    jsp探针 ver0.1

    jsp探针ceshi.jsp ; charset=gb2312" %> class LfSpy { boolean supportHibernate = false; boolean supportJNDI = false;...boolean supportJavaxSql = false;...private final String linuxParseMacAddress(String ...

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

    String planeImage = request.getSession().getServletContext().getRealPath("/image").replace("\\", "/")+"/"+"symark.png"; //获取目标图片的路径String targetPic = request.getSession().getServletContext()....

    struts2+spring2+ibates

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

    getServletContext()空指针异常的原因

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

    jsp 对文件的操作

    String path=request.getRealPath(""); path=path + "\Sub";//将要建立的目录路径 File d=new File(path);//建立代表Sub目录的File对象,并得到它的一个引用 if(d.exists()){//检查Sub目录是否存在

    fileutil工具类 处理文件流工具

    fileutil工具类 处理文件流工具 private static File file; /** * 判断文件是否存在 * * @param path * 文件路径 * @return boolean ... public static ....getRealPath(path)); return file.exists(); }

    图片上传filemover.jar,uploadbean.jar,cos.jar

    String sWebRootPath = request.getRealPath("/"); String sPath=sWebRootPath+"file"; String sLocalFileName = ""; int iFileCount = 0; if ( (files != null) || (!files.isEmpty()) ) { UploadFile file = ...

    java结合jsp写的上传文件代码

    dfif.setRepository(new File(request.getRealPath("/") + "ImagesUploadTemp"));// 设置存放临时文件的目录,web根目录下的ImagesUploadTemp目录 // 用以上工厂实例化上传组件 ServletFileUpload sfu = new ...

    JSF文件下载实现

    String realName = servletContext.getRealPath(path) + "/" + fileName; HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext .getCurrentInstance().getExternalContext()....

    java完整实现各种类型的文件上传和下载

    String path = this.getServletContext().getRealPath("/WEB-INF/upload"); 二、文件下载部分 1.访问地址一 http://localhost/UploadAndDownload/download.jsp 该地址实现的是通过超链接下载。 2.访问地址二 ...

    多附件上传与下载

    au.setSaveDir(this.servlet.getServletContext().getRealPath("/loadImage/"));//将图片放入存储文件路径 fileName=au.randomFileName(file.getFileName()); au.upload(au.getSaveDir()+File.separator+ ...

    ognl源码包值得用一用

    String path = request.getRealPath("/upload"); factory.setRepository(new File(path)); // 1mb factory.setSizeThreshold(1024 * 1024); // 实现文件上传的核心 ServletFileUpload upload = new ...

    图片上传下载

    File file new File request getRealPath " " + System currentTimeMillis ; 创建文件输出流 PrintStream ps new PrintStream new FileOutputStream file ; 接着开始读取文件内容 String ...

    JSP-文件图片上传-fileupload组件-commons-upload

    File file = new File(request.getRealPath("/") + "photofiles", tempFile.getName()); item.write(file); String fname = item.getName(); %> <script language="javascript"> alert("上传相片成功!"); ...

    iReport4.5打印实例

    .getRealPath("/"+jrprint)); JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),null,dataSource);//执行报表程序 程序中都很明了 大家肯定会问print.jsp怎么会调用到AppletServlet....

    java编写根据模板导出excel

    String path = servletContext.getRealPath("\\ExcelFile"); String srcFilePath = path + "\\template\\" + templateFile; String destFilePath = path + "\\download\\" + filename; Map beanParams =...

Global site tag (gtag.js) - Google Analytics