jsp页面取得物理路径的方法

来源:互联网 发布:mac .git文件夹 编辑:程序博客网 时间:2024/06/12 01:33
分三步取得
1:使用 内置对象 request 获取当前页面的虚拟路径,并剔除“/” 符号
    String webDir = request.getContextPath();
    webDir = webDir.substring(1);


2: 使用Application 内置对象获取真实路径
      String realPath = myApplication.getRealPath(s);


3: 使用java File 对象剔除文件名称,即可获取真实的目录
 String realDir=new File(realPath).getParent();




完整的方法例子如下:
public void setWebDir(HttpServletRequest request, ServletContext myApplication) {
   String webDir = request.getContextPath();
webDir = webDir.substring(1);
String realPath = myApplication.getRealPath(webDir );
String realDir=new File(realPath).getParent();
File dirImage = new File(realPath + "\\image");
}
0 0