在Servlet與JSP中取得當前文件所在的相對路徑與絕對路徑- -

 <%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.io.*" %>
<%
  //在Servlet與JSP中取得當前文件所在的相對路徑與絕對路徑
  
  //JSP中

  out.println("根目錄所對應的絕對路徑:" + request.getRequestURI() + "<br/>");
  
  String strPathFile = application.getRealPath(request.getRequestURI());
  out.println("文件的絕對路徑:" + strPathFile+ "<br/>");
out.println(application.getRealPath(request.getRequestURI()));
  String strDirPath = new File(application.getRealPath(request.getRequestURI())).getParent();
  out.println("目錄的絕對路徑:" + strDirPath + "<br/>");
%>

  //Servlet中
  //JSP中的application對象就是Servlet中的ServerContext,所以在Servlet中是如此獲得
  //import java.io.File;

System.out.println("根目錄所對應的絕對路徑:" + request.getServletPath() + "<br/>");

String strPathFile = request.getSession().getServletContext().getRealPath(request.getRequestURI());
System.out.println("文件的絕對路徑:" + strPathFile + "<br/>");

String strDirPath = new File(request.getSession().getServletContext().getRealPath(request.getRequestURI())).getParent();
System.out.println("目錄的絕對路徑:" + strDirPath + "<br/>");

  文件名不能包括以下字符://:*?"<>|

獲取Web項目的全路徑
String strDirPath = getServletContext().getRealPath("/");


你可以使用這個鏈接引用原文 http://publishblog.blogdriver.com/blog/tb.b?diaryID=504479
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章