Java Web solves the path (absolute path vs. relative path) problem
- 2020-05-27 05:30:27
Solve the path problem in Java Web:
There are two types of paths used in Java: absolute paths and relative paths. After all, Java essentially USES only absolute paths to find resources. All the relative paths to find resources are simply convenience methods. But API at the bottom helped us build the absolute path to find the resource!
When developing applications for Web, it is often necessary to get the physical path of the current WebRoot in the server.
If it is Servlet, Action, Controller, or Filter, Listener, interceptor or other relevant classes, we only need to get ServletContext, and then get the physical address of the current application on the server through ServletContext.getRealPath ("/").
If ServletContext is not available in a class, there are two ways to do this:
1. XXX.class.getClassLoader ().getResource(""); Method to get ClassPath, and then process to get the WebRoot directory. This can only take effect if the class is under WebRoot/ WEB-INF /classes. If the class is packaged into an jar file, this method will fail. Here's one way to do it.
2. spring framework, in WEB-INF/web.xml, create an param of webAppRootKey, specify a value (webapp.root by default) as the key value, and then execute String webAppRootKey = getServletContext().getRealPath("/") through Listener, or Filter, or Servlet; webapp.root corresponding to webAppRootKey is taken as Key respectively, and Value is written into System Properties system properties. The physical path of WebRoot is then obtained in the program by System.getProperty (" webapp.root ").
We can extend it one more time based on the second way of thinking. However, for applications deployed on one server, look again if that's not what you need.
Here are some ways to get classpath and the absolute path of the current class. You can use one of these methods to get the absolute path to the resource you need:
You get the URI directory for the current FileTest.class file. Not including myself!
You get the absolute URI path for the current classpath.
You also get the absolute URI path for the current ClassPath
You also get the absolute URI path for the current ClassPath.
5. Get the relative path of the server
For example: E: / apache - tomcat - 5.5.16 / apache - tomcat - 5.5.16 / bin
I recommend Thread.currentThread ().getContextClassLoader ().getResource ("") to get the URI representation of the current absolute path of classpath
6. Get the absolute path in the project
1 a with
Instead of using request.getRealPath ("/"), you can use the ServletContext.getRealPath ("/") method to get the absolute path to the root of the Web application
It is very easy to get the src file because src is the default relative directory. For example, if you say you want to get the test.java file in the com directory under src, that's all you need
File f = new File(com/test.java);
But what if I want to get a file that's not in src or WebRoot, but from a directory that's equivalent to src or WebRoot, say doc
My hard method works like this:
Thank you for reading, I hope to help you, thank you for your support of this site!