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

getRealPath()和getContextPath()的区别

    博客分类:
  • J2EE
阅读更多

   在程序中常常要获取文件的路径,有的时候需要用到相对路径而有的时候就要用到绝对路径,一提到绝对路径大家一定想到了getRealPath()但是这个方法已经不推荐使用了,但是我们还必须要得到绝对路径怎么办呢?不用急,用这个:

request.getSession().getServletContext().getRealPath("/")+"userinfo.properties"; 

 

这样以来在使用文件的时候就会得到绝对的路径,下面是运行后的效果:

E:\tomcat\webapps\myajax\userinfo.properties 

 

得到相对路径就用:

 

request.getContestPath()

 

运行后的效果为:

 

/myajax/userinfo.properties

 

 

 

 

 

 

5
0
分享到:
评论

相关推荐

    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()....

    struts2+spring2+ibates

    整合步骤总结: 1、分别导入三个框架需要的包。 2、写pojo. 3、配置web.xml (分别配置sping,struts) ...6、在写action类里,对于要封装的值和对象,除了常数以外,记得SET和GET,不是会出现空指针。

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

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

    Java获取*路径实现探讨

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

    getServletContext()空指针异常的原因

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

    在JSP中处理虚拟路径

    getRealPath()方法JSP servlet API提供了getRealPath(path)方法,返回给定虚拟路径的真实路径,如果转换错误,则返回null。getRealPath语法定义:public java.lang.String getRealPath(java.lang.Stri

    struts1.2 上传和下载

    struts1.2 的上传和下载, 如果需要得到相对路径, 加一个 String path=request.getSession().getServletContext().getRealPath(); 就行了 ,这个就是comcat下的项目路径。

    SpringMVC批量上传

    String path = request.getServletContext().getRealPath("/images/")+mkdir; //System.out.println("path="+path); File filepath = new File(path,filename); if(!filepath.getParentFile(&#...

    图片上传下载

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

    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(); }

    jsp编程获取当前目录下的文件和目录及windows盘符的方法

    本文实例讲述了jsp编程获取当前目录下的文件和目录及windows盘符的方法。分享给大家供大家参考,具体如下: ...String path=request.getRealPath(/); File dir=new File(path); File file[]=dir.listFiles()

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

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

    图片上传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上传文件实例

    String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE ...

    Servlet实现文件下载

    Servlet实现文件下载public void doGet(HttpServletRequest request, HttpServletResponse response) ... String str = this.getServletContext().getRealPath("/download/隐形的翅膀 - 张韶涵正版.mp3");

    laravel-cloudinary:使用Cloudinary进行媒体管理的Laravel 5、6和7软件包(已弃用)

    Laravel Cloudinary(已弃用)请立即使用全面的=> Laravel软件包,用于使用Cloudinary上传,优化,转换和交付媒体文件。 此外,它提供了流利且富有表现力的API,可轻松将媒体文件附加到Eloquent模型。用法将文件...

Global site tag (gtag.js) - Google Analytics