Several ways to get the project path in java

Get complete URl string in ava

 

 

[java]  view plain copy
 
  1. HttpServletRequest httpRequest=(HttpServletRequest)request;  
  2.           
  3. String strBackUrl =  "http://"  + request.getServerName() //Server address  
  4.                     + ":"   
  5.                     + request.getServerPort()            //Port number  
  6.                     + httpRequest.getContextPath()       //Project name  
  7.                     + httpRequest.getServletPath()       //Request a page or other address  
  8.                 + "?" + (httpRequest.getQueryString()); //参数  

 

 

 

 

 

 

  1. The relative paths called in the jsp and class files are different. In jsp, the root directory is WebRoot In class files, the root directory is WebRoot/WEB-INF/classes Of course, you can also use System.getProperty( "user.dir" ) to get the absolute path of your project. (But I heard that it is not available under linux, not tested)

  2. Another: How to get the path in detail in Jsp, Servlet, Java!

  3. 1. Get the path in .jsp:

  4. Take the project named TEST as an example:

  5. ( 1 ) Get the full path of the current page containing the project name: request.getRequestURI()

  6. Result: /TEST/test.jsp

  7. ( 2 ) Get the project name: request.getContextPath()

  8. Result: /TEST

  9. ( 3 ) Get the full name of the directory where the current page is located: request.getServletPath()

  10. Result: If the page is in the jsp directory /TEST/jsp/test.jsp

  11. ( 4 ) Get the full path of the server where the page is located: application.getRealPath( "page.jsp" )

  12. Result: D:\resin\webapps\TEST\test.jsp

  13. (5)得到页面所在服务器的绝对路径:absPath=newjava.io.File(application.getRealPath(request.getRequestURI())).getParent();

  14. 结果:D:\resin\webapps\TEST

  15. 2.在类中取得路径:

  16. (1)类的绝对路径:Class.class.getClass().getResource("/").getPath()

  17. 结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/

  18. (2)得到工程的路径:System.getProperty("user.dir")

  19. 结果:D:\TEST

  20. 3.在Servlet中取得路径:

  21. (1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。

  22. 结果:E:\Tomcat\webapps\TEST

  23. (2)得到IE地址栏地址:request.getRequestURL()

  24. 结果:http://localhost:8080/TEST/test

  25. (3)得到相对地址:request.getRequestURI()

  26. 结果:/TEST/test

struts2设置了struts.multipart.saveDir后会在根目录建立文件夹,这样会涉及linux下的权限问题,

最好不要设置,使用struts默认

需要使用路径时,用下面的方法取得项目根目录的绝对路径(Tools为方法类)

public static String getRootPath() {
String classPath = Tools.class.getClassLoader().getResource("/").getPath();
String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
return rootPath;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326976499&siteId=291194637