If it is to reflect the hierarchical sense of the project , When we use request When , It is generally used in the control layer , There is no need to request Also passed on to service use , So first of all, let's talk about controller Gets the absolute path of the current project
public Message<Object> uploadImgsFile(HttpServletRequest request) { //1 String
realPath =
request.getSession().getServletContext().getRealPath(File.separator);
System.out.println(realPath); //2 String realPath2 =
request.getServletContext().getRealPath(File.separator);
System.out.println(realPath2); return null; }
If you want to get the absolute path of the current project , And it's not here controller, At the same time, I don't want to pass it on request Parameters to service layer , You can do this
// code snippet // Now get the current path WebApplicationContext webApplicationContext =
ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext
= webApplicationContext.getServletContext(); String realPath =
servletContext.getRealPath(File.separator); System.out.println(realPath);
After the above operation, you can get the path of the project , In order to facilitate the needs of their own projects

Technology