1 用servlet獲取1.1 獲取專案的絕對路徑request.getSession().getServletContext().getRealPath("")1.
2 獲取瀏覽器地址request.getRequestURL()1.
3 獲取當前檔案的絕對路徑request.getSession().getServletContext().getRealPath(request.getRequestURI())2.獲取當前的classpath路徑String a2=類名.class.getResource("").toString();String a3=DBConnection.class.getResource("/").toString();String a4=DBConnection.class.getClassLoader().getResource("").toString();String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();//輸出很好理解3、獲取檔案的絕對路徑String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();int num=t.indexOf(".metadata");String path=t.substring(1,num).replace("/", "\\")+"專案名\\WebContent\\檔案";
1 用servlet獲取1.1 獲取專案的絕對路徑request.getSession().getServletContext().getRealPath("")1.
2 獲取瀏覽器地址request.getRequestURL()1.
3 獲取當前檔案的絕對路徑request.getSession().getServletContext().getRealPath(request.getRequestURI())2.獲取當前的classpath路徑String a2=類名.class.getResource("").toString();String a3=DBConnection.class.getResource("/").toString();String a4=DBConnection.class.getClassLoader().getResource("").toString();String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();//輸出很好理解3、獲取檔案的絕對路徑String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();int num=t.indexOf(".metadata");String path=t.substring(1,num).replace("/", "\\")+"專案名\\WebContent\\檔案";